nouveau_state.c 28 KB

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