nouveau_state.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * Copyright 2005 Stephane Marchesin
  3. * Copyright 2008 Stuart Bennett
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <linux/swab.h>
  26. #include "drmP.h"
  27. #include "drm.h"
  28. #include "drm_sarea.h"
  29. #include "drm_crtc_helper.h"
  30. #include <linux/vgaarb.h>
  31. #include "nouveau_drv.h"
  32. #include "nouveau_drm.h"
  33. #include "nv50_display.h"
  34. static int nouveau_stub_init(struct drm_device *dev) { return 0; }
  35. static void nouveau_stub_takedown(struct drm_device *dev) {}
  36. static int nouveau_init_engine_ptrs(struct drm_device *dev)
  37. {
  38. struct drm_nouveau_private *dev_priv = dev->dev_private;
  39. struct nouveau_engine *engine = &dev_priv->engine;
  40. switch (dev_priv->chipset & 0xf0) {
  41. case 0x00:
  42. engine->instmem.init = nv04_instmem_init;
  43. engine->instmem.takedown = nv04_instmem_takedown;
  44. engine->instmem.suspend = nv04_instmem_suspend;
  45. engine->instmem.resume = nv04_instmem_resume;
  46. engine->instmem.populate = nv04_instmem_populate;
  47. engine->instmem.clear = nv04_instmem_clear;
  48. engine->instmem.bind = nv04_instmem_bind;
  49. engine->instmem.unbind = nv04_instmem_unbind;
  50. engine->instmem.prepare_access = nv04_instmem_prepare_access;
  51. engine->instmem.finish_access = nv04_instmem_finish_access;
  52. engine->mc.init = nv04_mc_init;
  53. engine->mc.takedown = nv04_mc_takedown;
  54. engine->timer.init = nv04_timer_init;
  55. engine->timer.read = nv04_timer_read;
  56. engine->timer.takedown = nv04_timer_takedown;
  57. engine->fb.init = nv04_fb_init;
  58. engine->fb.takedown = nv04_fb_takedown;
  59. engine->graph.grclass = nv04_graph_grclass;
  60. engine->graph.init = nv04_graph_init;
  61. engine->graph.takedown = nv04_graph_takedown;
  62. engine->graph.fifo_access = nv04_graph_fifo_access;
  63. engine->graph.channel = nv04_graph_channel;
  64. engine->graph.create_context = nv04_graph_create_context;
  65. engine->graph.destroy_context = nv04_graph_destroy_context;
  66. engine->graph.load_context = nv04_graph_load_context;
  67. engine->graph.unload_context = nv04_graph_unload_context;
  68. engine->fifo.channels = 16;
  69. engine->fifo.init = nv04_fifo_init;
  70. engine->fifo.takedown = nouveau_stub_takedown;
  71. engine->fifo.disable = nv04_fifo_disable;
  72. engine->fifo.enable = nv04_fifo_enable;
  73. engine->fifo.reassign = nv04_fifo_reassign;
  74. engine->fifo.cache_flush = nv04_fifo_cache_flush;
  75. engine->fifo.cache_pull = nv04_fifo_cache_pull;
  76. engine->fifo.channel_id = nv04_fifo_channel_id;
  77. engine->fifo.create_context = nv04_fifo_create_context;
  78. engine->fifo.destroy_context = nv04_fifo_destroy_context;
  79. engine->fifo.load_context = nv04_fifo_load_context;
  80. engine->fifo.unload_context = nv04_fifo_unload_context;
  81. break;
  82. case 0x10:
  83. engine->instmem.init = nv04_instmem_init;
  84. engine->instmem.takedown = nv04_instmem_takedown;
  85. engine->instmem.suspend = nv04_instmem_suspend;
  86. engine->instmem.resume = nv04_instmem_resume;
  87. engine->instmem.populate = nv04_instmem_populate;
  88. engine->instmem.clear = nv04_instmem_clear;
  89. engine->instmem.bind = nv04_instmem_bind;
  90. engine->instmem.unbind = nv04_instmem_unbind;
  91. engine->instmem.prepare_access = nv04_instmem_prepare_access;
  92. engine->instmem.finish_access = nv04_instmem_finish_access;
  93. engine->mc.init = nv04_mc_init;
  94. engine->mc.takedown = nv04_mc_takedown;
  95. engine->timer.init = nv04_timer_init;
  96. engine->timer.read = nv04_timer_read;
  97. engine->timer.takedown = nv04_timer_takedown;
  98. engine->fb.init = nv10_fb_init;
  99. engine->fb.takedown = nv10_fb_takedown;
  100. engine->fb.set_region_tiling = nv10_fb_set_region_tiling;
  101. engine->graph.grclass = nv10_graph_grclass;
  102. engine->graph.init = nv10_graph_init;
  103. engine->graph.takedown = nv10_graph_takedown;
  104. engine->graph.channel = nv10_graph_channel;
  105. engine->graph.create_context = nv10_graph_create_context;
  106. engine->graph.destroy_context = nv10_graph_destroy_context;
  107. engine->graph.fifo_access = nv04_graph_fifo_access;
  108. engine->graph.load_context = nv10_graph_load_context;
  109. engine->graph.unload_context = nv10_graph_unload_context;
  110. engine->graph.set_region_tiling = nv10_graph_set_region_tiling;
  111. engine->fifo.channels = 32;
  112. engine->fifo.init = nv10_fifo_init;
  113. engine->fifo.takedown = nouveau_stub_takedown;
  114. engine->fifo.disable = nv04_fifo_disable;
  115. engine->fifo.enable = nv04_fifo_enable;
  116. engine->fifo.reassign = nv04_fifo_reassign;
  117. engine->fifo.cache_flush = nv04_fifo_cache_flush;
  118. engine->fifo.cache_pull = nv04_fifo_cache_pull;
  119. engine->fifo.channel_id = nv10_fifo_channel_id;
  120. engine->fifo.create_context = nv10_fifo_create_context;
  121. engine->fifo.destroy_context = nv10_fifo_destroy_context;
  122. engine->fifo.load_context = nv10_fifo_load_context;
  123. engine->fifo.unload_context = nv10_fifo_unload_context;
  124. break;
  125. case 0x20:
  126. engine->instmem.init = nv04_instmem_init;
  127. engine->instmem.takedown = nv04_instmem_takedown;
  128. engine->instmem.suspend = nv04_instmem_suspend;
  129. engine->instmem.resume = nv04_instmem_resume;
  130. engine->instmem.populate = nv04_instmem_populate;
  131. engine->instmem.clear = nv04_instmem_clear;
  132. engine->instmem.bind = nv04_instmem_bind;
  133. engine->instmem.unbind = nv04_instmem_unbind;
  134. engine->instmem.prepare_access = nv04_instmem_prepare_access;
  135. engine->instmem.finish_access = nv04_instmem_finish_access;
  136. engine->mc.init = nv04_mc_init;
  137. engine->mc.takedown = nv04_mc_takedown;
  138. engine->timer.init = nv04_timer_init;
  139. engine->timer.read = nv04_timer_read;
  140. engine->timer.takedown = nv04_timer_takedown;
  141. engine->fb.init = nv10_fb_init;
  142. engine->fb.takedown = nv10_fb_takedown;
  143. engine->fb.set_region_tiling = nv10_fb_set_region_tiling;
  144. engine->graph.grclass = nv20_graph_grclass;
  145. engine->graph.init = nv20_graph_init;
  146. engine->graph.takedown = nv20_graph_takedown;
  147. engine->graph.channel = nv10_graph_channel;
  148. engine->graph.create_context = nv20_graph_create_context;
  149. engine->graph.destroy_context = nv20_graph_destroy_context;
  150. engine->graph.fifo_access = nv04_graph_fifo_access;
  151. engine->graph.load_context = nv20_graph_load_context;
  152. engine->graph.unload_context = nv20_graph_unload_context;
  153. engine->graph.set_region_tiling = nv20_graph_set_region_tiling;
  154. engine->fifo.channels = 32;
  155. engine->fifo.init = nv10_fifo_init;
  156. engine->fifo.takedown = nouveau_stub_takedown;
  157. engine->fifo.disable = nv04_fifo_disable;
  158. engine->fifo.enable = nv04_fifo_enable;
  159. engine->fifo.reassign = nv04_fifo_reassign;
  160. engine->fifo.cache_flush = nv04_fifo_cache_flush;
  161. engine->fifo.cache_pull = nv04_fifo_cache_pull;
  162. engine->fifo.channel_id = nv10_fifo_channel_id;
  163. engine->fifo.create_context = nv10_fifo_create_context;
  164. engine->fifo.destroy_context = nv10_fifo_destroy_context;
  165. engine->fifo.load_context = nv10_fifo_load_context;
  166. engine->fifo.unload_context = nv10_fifo_unload_context;
  167. break;
  168. case 0x30:
  169. engine->instmem.init = nv04_instmem_init;
  170. engine->instmem.takedown = nv04_instmem_takedown;
  171. engine->instmem.suspend = nv04_instmem_suspend;
  172. engine->instmem.resume = nv04_instmem_resume;
  173. engine->instmem.populate = nv04_instmem_populate;
  174. engine->instmem.clear = nv04_instmem_clear;
  175. engine->instmem.bind = nv04_instmem_bind;
  176. engine->instmem.unbind = nv04_instmem_unbind;
  177. engine->instmem.prepare_access = nv04_instmem_prepare_access;
  178. engine->instmem.finish_access = nv04_instmem_finish_access;
  179. engine->mc.init = nv04_mc_init;
  180. engine->mc.takedown = nv04_mc_takedown;
  181. engine->timer.init = nv04_timer_init;
  182. engine->timer.read = nv04_timer_read;
  183. engine->timer.takedown = nv04_timer_takedown;
  184. engine->fb.init = nv10_fb_init;
  185. engine->fb.takedown = nv10_fb_takedown;
  186. engine->fb.set_region_tiling = nv10_fb_set_region_tiling;
  187. engine->graph.grclass = nv30_graph_grclass;
  188. engine->graph.init = nv30_graph_init;
  189. engine->graph.takedown = nv20_graph_takedown;
  190. engine->graph.fifo_access = nv04_graph_fifo_access;
  191. engine->graph.channel = nv10_graph_channel;
  192. engine->graph.create_context = nv20_graph_create_context;
  193. engine->graph.destroy_context = nv20_graph_destroy_context;
  194. engine->graph.load_context = nv20_graph_load_context;
  195. engine->graph.unload_context = nv20_graph_unload_context;
  196. engine->graph.set_region_tiling = nv20_graph_set_region_tiling;
  197. engine->fifo.channels = 32;
  198. engine->fifo.init = nv10_fifo_init;
  199. engine->fifo.takedown = nouveau_stub_takedown;
  200. engine->fifo.disable = nv04_fifo_disable;
  201. engine->fifo.enable = nv04_fifo_enable;
  202. engine->fifo.reassign = nv04_fifo_reassign;
  203. engine->fifo.cache_flush = nv04_fifo_cache_flush;
  204. engine->fifo.cache_pull = nv04_fifo_cache_pull;
  205. engine->fifo.channel_id = nv10_fifo_channel_id;
  206. engine->fifo.create_context = nv10_fifo_create_context;
  207. engine->fifo.destroy_context = nv10_fifo_destroy_context;
  208. engine->fifo.load_context = nv10_fifo_load_context;
  209. engine->fifo.unload_context = nv10_fifo_unload_context;
  210. break;
  211. case 0x40:
  212. case 0x60:
  213. engine->instmem.init = nv04_instmem_init;
  214. engine->instmem.takedown = nv04_instmem_takedown;
  215. engine->instmem.suspend = nv04_instmem_suspend;
  216. engine->instmem.resume = nv04_instmem_resume;
  217. engine->instmem.populate = nv04_instmem_populate;
  218. engine->instmem.clear = nv04_instmem_clear;
  219. engine->instmem.bind = nv04_instmem_bind;
  220. engine->instmem.unbind = nv04_instmem_unbind;
  221. engine->instmem.prepare_access = nv04_instmem_prepare_access;
  222. engine->instmem.finish_access = nv04_instmem_finish_access;
  223. engine->mc.init = nv40_mc_init;
  224. engine->mc.takedown = nv40_mc_takedown;
  225. engine->timer.init = nv04_timer_init;
  226. engine->timer.read = nv04_timer_read;
  227. engine->timer.takedown = nv04_timer_takedown;
  228. engine->fb.init = nv40_fb_init;
  229. engine->fb.takedown = nv40_fb_takedown;
  230. engine->fb.set_region_tiling = nv40_fb_set_region_tiling;
  231. engine->graph.grclass = nv40_graph_grclass;
  232. engine->graph.init = nv40_graph_init;
  233. engine->graph.takedown = nv40_graph_takedown;
  234. engine->graph.fifo_access = nv04_graph_fifo_access;
  235. engine->graph.channel = nv40_graph_channel;
  236. engine->graph.create_context = nv40_graph_create_context;
  237. engine->graph.destroy_context = nv40_graph_destroy_context;
  238. engine->graph.load_context = nv40_graph_load_context;
  239. engine->graph.unload_context = nv40_graph_unload_context;
  240. engine->graph.set_region_tiling = nv40_graph_set_region_tiling;
  241. engine->fifo.channels = 32;
  242. engine->fifo.init = nv40_fifo_init;
  243. engine->fifo.takedown = nouveau_stub_takedown;
  244. engine->fifo.disable = nv04_fifo_disable;
  245. engine->fifo.enable = nv04_fifo_enable;
  246. engine->fifo.reassign = nv04_fifo_reassign;
  247. engine->fifo.cache_flush = nv04_fifo_cache_flush;
  248. engine->fifo.cache_pull = nv04_fifo_cache_pull;
  249. engine->fifo.channel_id = nv10_fifo_channel_id;
  250. engine->fifo.create_context = nv40_fifo_create_context;
  251. engine->fifo.destroy_context = nv40_fifo_destroy_context;
  252. engine->fifo.load_context = nv40_fifo_load_context;
  253. engine->fifo.unload_context = nv40_fifo_unload_context;
  254. break;
  255. case 0x50:
  256. case 0x80: /* gotta love NVIDIA's consistency.. */
  257. case 0x90:
  258. case 0xA0:
  259. engine->instmem.init = nv50_instmem_init;
  260. engine->instmem.takedown = nv50_instmem_takedown;
  261. engine->instmem.suspend = nv50_instmem_suspend;
  262. engine->instmem.resume = nv50_instmem_resume;
  263. engine->instmem.populate = nv50_instmem_populate;
  264. engine->instmem.clear = nv50_instmem_clear;
  265. engine->instmem.bind = nv50_instmem_bind;
  266. engine->instmem.unbind = nv50_instmem_unbind;
  267. engine->instmem.prepare_access = nv50_instmem_prepare_access;
  268. engine->instmem.finish_access = nv50_instmem_finish_access;
  269. engine->mc.init = nv50_mc_init;
  270. engine->mc.takedown = nv50_mc_takedown;
  271. engine->timer.init = nv04_timer_init;
  272. engine->timer.read = nv04_timer_read;
  273. engine->timer.takedown = nv04_timer_takedown;
  274. engine->fb.init = nouveau_stub_init;
  275. engine->fb.takedown = nouveau_stub_takedown;
  276. engine->graph.grclass = nv50_graph_grclass;
  277. engine->graph.init = nv50_graph_init;
  278. engine->graph.takedown = nv50_graph_takedown;
  279. engine->graph.fifo_access = nv50_graph_fifo_access;
  280. engine->graph.channel = nv50_graph_channel;
  281. engine->graph.create_context = nv50_graph_create_context;
  282. engine->graph.destroy_context = nv50_graph_destroy_context;
  283. engine->graph.load_context = nv50_graph_load_context;
  284. engine->graph.unload_context = nv50_graph_unload_context;
  285. engine->fifo.channels = 128;
  286. engine->fifo.init = nv50_fifo_init;
  287. engine->fifo.takedown = nv50_fifo_takedown;
  288. engine->fifo.disable = nv04_fifo_disable;
  289. engine->fifo.enable = nv04_fifo_enable;
  290. engine->fifo.reassign = nv04_fifo_reassign;
  291. engine->fifo.channel_id = nv50_fifo_channel_id;
  292. engine->fifo.create_context = nv50_fifo_create_context;
  293. engine->fifo.destroy_context = nv50_fifo_destroy_context;
  294. engine->fifo.load_context = nv50_fifo_load_context;
  295. engine->fifo.unload_context = nv50_fifo_unload_context;
  296. break;
  297. default:
  298. NV_ERROR(dev, "NV%02x unsupported\n", dev_priv->chipset);
  299. return 1;
  300. }
  301. return 0;
  302. }
  303. static unsigned int
  304. nouveau_vga_set_decode(void *priv, bool state)
  305. {
  306. struct drm_device *dev = priv;
  307. struct drm_nouveau_private *dev_priv = dev->dev_private;
  308. if (dev_priv->chipset >= 0x40)
  309. nv_wr32(dev, 0x88054, state);
  310. else
  311. nv_wr32(dev, 0x1854, state);
  312. if (state)
  313. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  314. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  315. else
  316. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  317. }
  318. static int
  319. nouveau_card_init_channel(struct drm_device *dev)
  320. {
  321. struct drm_nouveau_private *dev_priv = dev->dev_private;
  322. struct nouveau_gpuobj *gpuobj;
  323. int ret;
  324. ret = nouveau_channel_alloc(dev, &dev_priv->channel,
  325. (struct drm_file *)-2,
  326. NvDmaFB, NvDmaTT);
  327. if (ret)
  328. return ret;
  329. gpuobj = NULL;
  330. ret = nouveau_gpuobj_dma_new(dev_priv->channel, NV_CLASS_DMA_IN_MEMORY,
  331. 0, nouveau_mem_fb_amount(dev),
  332. NV_DMA_ACCESS_RW, NV_DMA_TARGET_VIDMEM,
  333. &gpuobj);
  334. if (ret)
  335. goto out_err;
  336. ret = nouveau_gpuobj_ref_add(dev, dev_priv->channel, NvDmaVRAM,
  337. gpuobj, NULL);
  338. if (ret)
  339. goto out_err;
  340. gpuobj = NULL;
  341. ret = nouveau_gpuobj_gart_dma_new(dev_priv->channel, 0,
  342. dev_priv->gart_info.aper_size,
  343. NV_DMA_ACCESS_RW, &gpuobj, NULL);
  344. if (ret)
  345. goto out_err;
  346. ret = nouveau_gpuobj_ref_add(dev, dev_priv->channel, NvDmaGART,
  347. gpuobj, NULL);
  348. if (ret)
  349. goto out_err;
  350. return 0;
  351. out_err:
  352. nouveau_gpuobj_del(dev, &gpuobj);
  353. nouveau_channel_free(dev_priv->channel);
  354. dev_priv->channel = NULL;
  355. return ret;
  356. }
  357. int
  358. nouveau_card_init(struct drm_device *dev)
  359. {
  360. struct drm_nouveau_private *dev_priv = dev->dev_private;
  361. struct nouveau_engine *engine;
  362. int ret;
  363. NV_DEBUG(dev, "prev state = %d\n", dev_priv->init_state);
  364. if (dev_priv->init_state == NOUVEAU_CARD_INIT_DONE)
  365. return 0;
  366. vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
  367. /* Initialise internal driver API hooks */
  368. ret = nouveau_init_engine_ptrs(dev);
  369. if (ret)
  370. goto out;
  371. engine = &dev_priv->engine;
  372. dev_priv->init_state = NOUVEAU_CARD_INIT_FAILED;
  373. spin_lock_init(&dev_priv->context_switch_lock);
  374. /* Parse BIOS tables / Run init tables if card not POSTed */
  375. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  376. ret = nouveau_bios_init(dev);
  377. if (ret)
  378. goto out;
  379. }
  380. ret = nouveau_gpuobj_early_init(dev);
  381. if (ret)
  382. goto out_bios;
  383. /* Initialise instance memory, must happen before mem_init so we
  384. * know exactly how much VRAM we're able to use for "normal"
  385. * purposes.
  386. */
  387. ret = engine->instmem.init(dev);
  388. if (ret)
  389. goto out_gpuobj_early;
  390. /* Setup the memory manager */
  391. ret = nouveau_mem_init(dev);
  392. if (ret)
  393. goto out_instmem;
  394. ret = nouveau_gpuobj_init(dev);
  395. if (ret)
  396. goto out_mem;
  397. /* PMC */
  398. ret = engine->mc.init(dev);
  399. if (ret)
  400. goto out_gpuobj;
  401. /* PTIMER */
  402. ret = engine->timer.init(dev);
  403. if (ret)
  404. goto out_mc;
  405. /* PFB */
  406. ret = engine->fb.init(dev);
  407. if (ret)
  408. goto out_timer;
  409. if (nouveau_noaccel)
  410. engine->graph.accel_blocked = true;
  411. else {
  412. /* PGRAPH */
  413. ret = engine->graph.init(dev);
  414. if (ret)
  415. goto out_fb;
  416. /* PFIFO */
  417. ret = engine->fifo.init(dev);
  418. if (ret)
  419. goto out_graph;
  420. }
  421. /* this call irq_preinstall, register irq handler and
  422. * call irq_postinstall
  423. */
  424. ret = drm_irq_install(dev);
  425. if (ret)
  426. goto out_fifo;
  427. ret = drm_vblank_init(dev, 0);
  428. if (ret)
  429. goto out_irq;
  430. /* what about PVIDEO/PCRTC/PRAMDAC etc? */
  431. if (!engine->graph.accel_blocked) {
  432. ret = nouveau_card_init_channel(dev);
  433. if (ret)
  434. goto out_irq;
  435. }
  436. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  437. if (dev_priv->card_type >= NV_50)
  438. ret = nv50_display_create(dev);
  439. else
  440. ret = nv04_display_create(dev);
  441. if (ret)
  442. goto out_irq;
  443. }
  444. ret = nouveau_backlight_init(dev);
  445. if (ret)
  446. NV_ERROR(dev, "Error %d registering backlight\n", ret);
  447. dev_priv->init_state = NOUVEAU_CARD_INIT_DONE;
  448. if (drm_core_check_feature(dev, DRIVER_MODESET))
  449. drm_helper_initial_config(dev);
  450. return 0;
  451. out_irq:
  452. drm_irq_uninstall(dev);
  453. out_fifo:
  454. if (!nouveau_noaccel)
  455. engine->fifo.takedown(dev);
  456. out_graph:
  457. if (!nouveau_noaccel)
  458. engine->graph.takedown(dev);
  459. out_fb:
  460. engine->fb.takedown(dev);
  461. out_timer:
  462. engine->timer.takedown(dev);
  463. out_mc:
  464. engine->mc.takedown(dev);
  465. out_gpuobj:
  466. nouveau_gpuobj_takedown(dev);
  467. out_mem:
  468. nouveau_mem_close(dev);
  469. out_instmem:
  470. engine->instmem.takedown(dev);
  471. out_gpuobj_early:
  472. nouveau_gpuobj_late_takedown(dev);
  473. out_bios:
  474. nouveau_bios_takedown(dev);
  475. out:
  476. vga_client_register(dev->pdev, NULL, NULL, NULL);
  477. return ret;
  478. }
  479. static void nouveau_card_takedown(struct drm_device *dev)
  480. {
  481. struct drm_nouveau_private *dev_priv = dev->dev_private;
  482. struct nouveau_engine *engine = &dev_priv->engine;
  483. NV_DEBUG(dev, "prev state = %d\n", dev_priv->init_state);
  484. if (dev_priv->init_state != NOUVEAU_CARD_INIT_DOWN) {
  485. nouveau_backlight_exit(dev);
  486. if (dev_priv->channel) {
  487. nouveau_channel_free(dev_priv->channel);
  488. dev_priv->channel = NULL;
  489. }
  490. if (!nouveau_noaccel) {
  491. engine->fifo.takedown(dev);
  492. engine->graph.takedown(dev);
  493. }
  494. engine->fb.takedown(dev);
  495. engine->timer.takedown(dev);
  496. engine->mc.takedown(dev);
  497. mutex_lock(&dev->struct_mutex);
  498. ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
  499. ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
  500. mutex_unlock(&dev->struct_mutex);
  501. nouveau_sgdma_takedown(dev);
  502. nouveau_gpuobj_takedown(dev);
  503. nouveau_mem_close(dev);
  504. engine->instmem.takedown(dev);
  505. if (drm_core_check_feature(dev, DRIVER_MODESET))
  506. drm_irq_uninstall(dev);
  507. nouveau_gpuobj_late_takedown(dev);
  508. nouveau_bios_takedown(dev);
  509. vga_client_register(dev->pdev, NULL, NULL, NULL);
  510. dev_priv->init_state = NOUVEAU_CARD_INIT_DOWN;
  511. }
  512. }
  513. /* here a client dies, release the stuff that was allocated for its
  514. * file_priv */
  515. void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
  516. {
  517. nouveau_channel_cleanup(dev, file_priv);
  518. }
  519. /* first module load, setup the mmio/fb mapping */
  520. /* KMS: we need mmio at load time, not when the first drm client opens. */
  521. int nouveau_firstopen(struct drm_device *dev)
  522. {
  523. return 0;
  524. }
  525. /* if we have an OF card, copy vbios to RAMIN */
  526. static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
  527. {
  528. #if defined(__powerpc__)
  529. int size, i;
  530. const uint32_t *bios;
  531. struct device_node *dn = pci_device_to_OF_node(dev->pdev);
  532. if (!dn) {
  533. NV_INFO(dev, "Unable to get the OF node\n");
  534. return;
  535. }
  536. bios = of_get_property(dn, "NVDA,BMP", &size);
  537. if (bios) {
  538. for (i = 0; i < size; i += 4)
  539. nv_wi32(dev, i, bios[i/4]);
  540. NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
  541. } else {
  542. NV_INFO(dev, "Unable to get the OF bios\n");
  543. }
  544. #endif
  545. }
  546. int nouveau_load(struct drm_device *dev, unsigned long flags)
  547. {
  548. struct drm_nouveau_private *dev_priv;
  549. uint32_t reg0;
  550. resource_size_t mmio_start_offs;
  551. dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
  552. if (!dev_priv)
  553. return -ENOMEM;
  554. dev->dev_private = dev_priv;
  555. dev_priv->dev = dev;
  556. dev_priv->flags = flags & NOUVEAU_FLAGS;
  557. dev_priv->init_state = NOUVEAU_CARD_INIT_DOWN;
  558. NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
  559. dev->pci_vendor, dev->pci_device, dev->pdev->class);
  560. dev_priv->acpi_dsm = nouveau_dsm_probe(dev);
  561. if (dev_priv->acpi_dsm)
  562. nouveau_hybrid_setup(dev);
  563. dev_priv->wq = create_workqueue("nouveau");
  564. if (!dev_priv->wq)
  565. return -EINVAL;
  566. /* resource 0 is mmio regs */
  567. /* resource 1 is linear FB */
  568. /* resource 2 is RAMIN (mmio regs + 0x1000000) */
  569. /* resource 6 is bios */
  570. /* map the mmio regs */
  571. mmio_start_offs = pci_resource_start(dev->pdev, 0);
  572. dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
  573. if (!dev_priv->mmio) {
  574. NV_ERROR(dev, "Unable to initialize the mmio mapping. "
  575. "Please report your setup to " DRIVER_EMAIL "\n");
  576. return -EINVAL;
  577. }
  578. NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
  579. (unsigned long long)mmio_start_offs);
  580. #ifdef __BIG_ENDIAN
  581. /* Put the card in BE mode if it's not */
  582. if (nv_rd32(dev, NV03_PMC_BOOT_1))
  583. nv_wr32(dev, NV03_PMC_BOOT_1, 0x00000001);
  584. DRM_MEMORYBARRIER();
  585. #endif
  586. /* Time to determine the card architecture */
  587. reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
  588. /* We're dealing with >=NV10 */
  589. if ((reg0 & 0x0f000000) > 0) {
  590. /* Bit 27-20 contain the architecture in hex */
  591. dev_priv->chipset = (reg0 & 0xff00000) >> 20;
  592. /* NV04 or NV05 */
  593. } else if ((reg0 & 0xff00fff0) == 0x20004000) {
  594. if (reg0 & 0x00f00000)
  595. dev_priv->chipset = 0x05;
  596. else
  597. dev_priv->chipset = 0x04;
  598. } else
  599. dev_priv->chipset = 0xff;
  600. switch (dev_priv->chipset & 0xf0) {
  601. case 0x00:
  602. case 0x10:
  603. case 0x20:
  604. case 0x30:
  605. dev_priv->card_type = dev_priv->chipset & 0xf0;
  606. break;
  607. case 0x40:
  608. case 0x60:
  609. dev_priv->card_type = NV_40;
  610. break;
  611. case 0x50:
  612. case 0x80:
  613. case 0x90:
  614. case 0xa0:
  615. dev_priv->card_type = NV_50;
  616. break;
  617. default:
  618. NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
  619. return -EINVAL;
  620. }
  621. NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
  622. dev_priv->card_type, reg0);
  623. /* map larger RAMIN aperture on NV40 cards */
  624. dev_priv->ramin = NULL;
  625. if (dev_priv->card_type >= NV_40) {
  626. int ramin_bar = 2;
  627. if (pci_resource_len(dev->pdev, ramin_bar) == 0)
  628. ramin_bar = 3;
  629. dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
  630. dev_priv->ramin = ioremap(
  631. pci_resource_start(dev->pdev, ramin_bar),
  632. dev_priv->ramin_size);
  633. if (!dev_priv->ramin) {
  634. NV_ERROR(dev, "Failed to init RAMIN mapping, "
  635. "limited instance memory available\n");
  636. }
  637. }
  638. /* On older cards (or if the above failed), create a map covering
  639. * the BAR0 PRAMIN aperture */
  640. if (!dev_priv->ramin) {
  641. dev_priv->ramin_size = 1 * 1024 * 1024;
  642. dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
  643. dev_priv->ramin_size);
  644. if (!dev_priv->ramin) {
  645. NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
  646. return -ENOMEM;
  647. }
  648. }
  649. nouveau_OF_copy_vbios_to_ramin(dev);
  650. /* Special flags */
  651. if (dev->pci_device == 0x01a0)
  652. dev_priv->flags |= NV_NFORCE;
  653. else if (dev->pci_device == 0x01f0)
  654. dev_priv->flags |= NV_NFORCE2;
  655. /* For kernel modesetting, init card now and bring up fbcon */
  656. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  657. int ret = nouveau_card_init(dev);
  658. if (ret)
  659. return ret;
  660. }
  661. return 0;
  662. }
  663. static void nouveau_close(struct drm_device *dev)
  664. {
  665. struct drm_nouveau_private *dev_priv = dev->dev_private;
  666. /* In the case of an error dev_priv may not be allocated yet */
  667. if (dev_priv)
  668. nouveau_card_takedown(dev);
  669. }
  670. /* KMS: we need mmio at load time, not when the first drm client opens. */
  671. void nouveau_lastclose(struct drm_device *dev)
  672. {
  673. if (drm_core_check_feature(dev, DRIVER_MODESET))
  674. return;
  675. nouveau_close(dev);
  676. }
  677. int nouveau_unload(struct drm_device *dev)
  678. {
  679. struct drm_nouveau_private *dev_priv = dev->dev_private;
  680. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  681. if (dev_priv->card_type >= NV_50)
  682. nv50_display_destroy(dev);
  683. else
  684. nv04_display_destroy(dev);
  685. nouveau_close(dev);
  686. }
  687. iounmap(dev_priv->mmio);
  688. iounmap(dev_priv->ramin);
  689. kfree(dev_priv);
  690. dev->dev_private = NULL;
  691. return 0;
  692. }
  693. int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
  694. struct drm_file *file_priv)
  695. {
  696. struct drm_nouveau_private *dev_priv = dev->dev_private;
  697. struct drm_nouveau_getparam *getparam = data;
  698. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  699. switch (getparam->param) {
  700. case NOUVEAU_GETPARAM_CHIPSET_ID:
  701. getparam->value = dev_priv->chipset;
  702. break;
  703. case NOUVEAU_GETPARAM_PCI_VENDOR:
  704. getparam->value = dev->pci_vendor;
  705. break;
  706. case NOUVEAU_GETPARAM_PCI_DEVICE:
  707. getparam->value = dev->pci_device;
  708. break;
  709. case NOUVEAU_GETPARAM_BUS_TYPE:
  710. if (drm_device_is_agp(dev))
  711. getparam->value = NV_AGP;
  712. else if (drm_device_is_pcie(dev))
  713. getparam->value = NV_PCIE;
  714. else
  715. getparam->value = NV_PCI;
  716. break;
  717. case NOUVEAU_GETPARAM_FB_PHYSICAL:
  718. getparam->value = dev_priv->fb_phys;
  719. break;
  720. case NOUVEAU_GETPARAM_AGP_PHYSICAL:
  721. getparam->value = dev_priv->gart_info.aper_base;
  722. break;
  723. case NOUVEAU_GETPARAM_PCI_PHYSICAL:
  724. if (dev->sg) {
  725. getparam->value = (unsigned long)dev->sg->virtual;
  726. } else {
  727. NV_ERROR(dev, "Requested PCIGART address, "
  728. "while no PCIGART was created\n");
  729. return -EINVAL;
  730. }
  731. break;
  732. case NOUVEAU_GETPARAM_FB_SIZE:
  733. getparam->value = dev_priv->fb_available_size;
  734. break;
  735. case NOUVEAU_GETPARAM_AGP_SIZE:
  736. getparam->value = dev_priv->gart_info.aper_size;
  737. break;
  738. case NOUVEAU_GETPARAM_VM_VRAM_BASE:
  739. getparam->value = dev_priv->vm_vram_base;
  740. break;
  741. case NOUVEAU_GETPARAM_GRAPH_UNITS:
  742. /* NV40 and NV50 versions are quite different, but register
  743. * address is the same. User is supposed to know the card
  744. * family anyway... */
  745. if (dev_priv->chipset >= 0x40) {
  746. getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
  747. break;
  748. }
  749. /* FALLTHRU */
  750. default:
  751. NV_ERROR(dev, "unknown parameter %lld\n", getparam->param);
  752. return -EINVAL;
  753. }
  754. return 0;
  755. }
  756. int
  757. nouveau_ioctl_setparam(struct drm_device *dev, void *data,
  758. struct drm_file *file_priv)
  759. {
  760. struct drm_nouveau_setparam *setparam = data;
  761. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  762. switch (setparam->param) {
  763. default:
  764. NV_ERROR(dev, "unknown parameter %lld\n", setparam->param);
  765. return -EINVAL;
  766. }
  767. return 0;
  768. }
  769. /* Wait until (value(reg) & mask) == val, up until timeout has hit */
  770. bool nouveau_wait_until(struct drm_device *dev, uint64_t timeout,
  771. uint32_t reg, uint32_t mask, uint32_t val)
  772. {
  773. struct drm_nouveau_private *dev_priv = dev->dev_private;
  774. struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
  775. uint64_t start = ptimer->read(dev);
  776. do {
  777. if ((nv_rd32(dev, reg) & mask) == val)
  778. return true;
  779. } while (ptimer->read(dev) - start < timeout);
  780. return false;
  781. }
  782. /* Waits for PGRAPH to go completely idle */
  783. bool nouveau_wait_for_idle(struct drm_device *dev)
  784. {
  785. if (!nv_wait(NV04_PGRAPH_STATUS, 0xffffffff, 0x00000000)) {
  786. NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
  787. nv_rd32(dev, NV04_PGRAPH_STATUS));
  788. return false;
  789. }
  790. return true;
  791. }