nouveau_state.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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 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 = nv50_fb_init;
  275. engine->fb.takedown = nv50_fb_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, dev_priv->vram_size,
  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. static void nouveau_switcheroo_set_state(struct pci_dev *pdev,
  358. enum vga_switcheroo_state state)
  359. {
  360. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  361. if (state == VGA_SWITCHEROO_ON) {
  362. printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
  363. nouveau_pci_resume(pdev);
  364. } else {
  365. printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
  366. nouveau_pci_suspend(pdev, pmm);
  367. }
  368. }
  369. static bool nouveau_switcheroo_can_switch(struct pci_dev *pdev)
  370. {
  371. struct drm_device *dev = pci_get_drvdata(pdev);
  372. bool can_switch;
  373. spin_lock(&dev->count_lock);
  374. can_switch = (dev->open_count == 0);
  375. spin_unlock(&dev->count_lock);
  376. return can_switch;
  377. }
  378. int
  379. nouveau_card_init(struct drm_device *dev)
  380. {
  381. struct drm_nouveau_private *dev_priv = dev->dev_private;
  382. struct nouveau_engine *engine;
  383. int ret;
  384. NV_DEBUG(dev, "prev state = %d\n", dev_priv->init_state);
  385. if (dev_priv->init_state == NOUVEAU_CARD_INIT_DONE)
  386. return 0;
  387. vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
  388. vga_switcheroo_register_client(dev->pdev, nouveau_switcheroo_set_state,
  389. nouveau_switcheroo_can_switch);
  390. /* Initialise internal driver API hooks */
  391. ret = nouveau_init_engine_ptrs(dev);
  392. if (ret)
  393. goto out;
  394. engine = &dev_priv->engine;
  395. dev_priv->init_state = NOUVEAU_CARD_INIT_FAILED;
  396. spin_lock_init(&dev_priv->context_switch_lock);
  397. /* Parse BIOS tables / Run init tables if card not POSTed */
  398. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  399. ret = nouveau_bios_init(dev);
  400. if (ret)
  401. goto out;
  402. }
  403. ret = nouveau_mem_detect(dev);
  404. if (ret)
  405. goto out_bios;
  406. ret = nouveau_gpuobj_early_init(dev);
  407. if (ret)
  408. goto out_bios;
  409. /* Initialise instance memory, must happen before mem_init so we
  410. * know exactly how much VRAM we're able to use for "normal"
  411. * purposes.
  412. */
  413. ret = engine->instmem.init(dev);
  414. if (ret)
  415. goto out_gpuobj_early;
  416. /* Setup the memory manager */
  417. ret = nouveau_mem_init(dev);
  418. if (ret)
  419. goto out_instmem;
  420. ret = nouveau_gpuobj_init(dev);
  421. if (ret)
  422. goto out_mem;
  423. /* PMC */
  424. ret = engine->mc.init(dev);
  425. if (ret)
  426. goto out_gpuobj;
  427. /* PTIMER */
  428. ret = engine->timer.init(dev);
  429. if (ret)
  430. goto out_mc;
  431. /* PFB */
  432. ret = engine->fb.init(dev);
  433. if (ret)
  434. goto out_timer;
  435. if (nouveau_noaccel)
  436. engine->graph.accel_blocked = true;
  437. else {
  438. /* PGRAPH */
  439. ret = engine->graph.init(dev);
  440. if (ret)
  441. goto out_fb;
  442. /* PFIFO */
  443. ret = engine->fifo.init(dev);
  444. if (ret)
  445. goto out_graph;
  446. }
  447. /* this call irq_preinstall, register irq handler and
  448. * call irq_postinstall
  449. */
  450. ret = drm_irq_install(dev);
  451. if (ret)
  452. goto out_fifo;
  453. ret = drm_vblank_init(dev, 0);
  454. if (ret)
  455. goto out_irq;
  456. /* what about PVIDEO/PCRTC/PRAMDAC etc? */
  457. if (!engine->graph.accel_blocked) {
  458. ret = nouveau_card_init_channel(dev);
  459. if (ret)
  460. goto out_irq;
  461. }
  462. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  463. if (dev_priv->card_type >= NV_50)
  464. ret = nv50_display_create(dev);
  465. else
  466. ret = nv04_display_create(dev);
  467. if (ret)
  468. goto out_channel;
  469. }
  470. ret = nouveau_backlight_init(dev);
  471. if (ret)
  472. NV_ERROR(dev, "Error %d registering backlight\n", ret);
  473. dev_priv->init_state = NOUVEAU_CARD_INIT_DONE;
  474. if (drm_core_check_feature(dev, DRIVER_MODESET))
  475. drm_helper_initial_config(dev);
  476. return 0;
  477. out_channel:
  478. if (dev_priv->channel) {
  479. nouveau_channel_free(dev_priv->channel);
  480. dev_priv->channel = NULL;
  481. }
  482. out_irq:
  483. drm_irq_uninstall(dev);
  484. out_fifo:
  485. if (!nouveau_noaccel)
  486. engine->fifo.takedown(dev);
  487. out_graph:
  488. if (!nouveau_noaccel)
  489. engine->graph.takedown(dev);
  490. out_fb:
  491. engine->fb.takedown(dev);
  492. out_timer:
  493. engine->timer.takedown(dev);
  494. out_mc:
  495. engine->mc.takedown(dev);
  496. out_gpuobj:
  497. nouveau_gpuobj_takedown(dev);
  498. out_mem:
  499. nouveau_sgdma_takedown(dev);
  500. nouveau_mem_close(dev);
  501. out_instmem:
  502. engine->instmem.takedown(dev);
  503. out_gpuobj_early:
  504. nouveau_gpuobj_late_takedown(dev);
  505. out_bios:
  506. nouveau_bios_takedown(dev);
  507. out:
  508. vga_client_register(dev->pdev, NULL, NULL, NULL);
  509. return ret;
  510. }
  511. static void nouveau_card_takedown(struct drm_device *dev)
  512. {
  513. struct drm_nouveau_private *dev_priv = dev->dev_private;
  514. struct nouveau_engine *engine = &dev_priv->engine;
  515. NV_DEBUG(dev, "prev state = %d\n", dev_priv->init_state);
  516. if (dev_priv->init_state != NOUVEAU_CARD_INIT_DOWN) {
  517. nouveau_backlight_exit(dev);
  518. if (dev_priv->channel) {
  519. nouveau_channel_free(dev_priv->channel);
  520. dev_priv->channel = NULL;
  521. }
  522. if (!nouveau_noaccel) {
  523. engine->fifo.takedown(dev);
  524. engine->graph.takedown(dev);
  525. }
  526. engine->fb.takedown(dev);
  527. engine->timer.takedown(dev);
  528. engine->mc.takedown(dev);
  529. mutex_lock(&dev->struct_mutex);
  530. ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
  531. ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
  532. mutex_unlock(&dev->struct_mutex);
  533. nouveau_sgdma_takedown(dev);
  534. nouveau_gpuobj_takedown(dev);
  535. nouveau_mem_close(dev);
  536. engine->instmem.takedown(dev);
  537. if (drm_core_check_feature(dev, DRIVER_MODESET))
  538. drm_irq_uninstall(dev);
  539. nouveau_gpuobj_late_takedown(dev);
  540. nouveau_bios_takedown(dev);
  541. vga_client_register(dev->pdev, NULL, NULL, NULL);
  542. dev_priv->init_state = NOUVEAU_CARD_INIT_DOWN;
  543. }
  544. }
  545. /* here a client dies, release the stuff that was allocated for its
  546. * file_priv */
  547. void nouveau_preclose(struct drm_device *dev, struct drm_file *file_priv)
  548. {
  549. nouveau_channel_cleanup(dev, file_priv);
  550. }
  551. /* first module load, setup the mmio/fb mapping */
  552. /* KMS: we need mmio at load time, not when the first drm client opens. */
  553. int nouveau_firstopen(struct drm_device *dev)
  554. {
  555. return 0;
  556. }
  557. /* if we have an OF card, copy vbios to RAMIN */
  558. static void nouveau_OF_copy_vbios_to_ramin(struct drm_device *dev)
  559. {
  560. #if defined(__powerpc__)
  561. int size, i;
  562. const uint32_t *bios;
  563. struct device_node *dn = pci_device_to_OF_node(dev->pdev);
  564. if (!dn) {
  565. NV_INFO(dev, "Unable to get the OF node\n");
  566. return;
  567. }
  568. bios = of_get_property(dn, "NVDA,BMP", &size);
  569. if (bios) {
  570. for (i = 0; i < size; i += 4)
  571. nv_wi32(dev, i, bios[i/4]);
  572. NV_INFO(dev, "OF bios successfully copied (%d bytes)\n", size);
  573. } else {
  574. NV_INFO(dev, "Unable to get the OF bios\n");
  575. }
  576. #endif
  577. }
  578. int nouveau_load(struct drm_device *dev, unsigned long flags)
  579. {
  580. struct drm_nouveau_private *dev_priv;
  581. uint32_t reg0;
  582. resource_size_t mmio_start_offs;
  583. dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
  584. if (!dev_priv)
  585. return -ENOMEM;
  586. dev->dev_private = dev_priv;
  587. dev_priv->dev = dev;
  588. dev_priv->flags = flags & NOUVEAU_FLAGS;
  589. dev_priv->init_state = NOUVEAU_CARD_INIT_DOWN;
  590. NV_DEBUG(dev, "vendor: 0x%X device: 0x%X class: 0x%X\n",
  591. dev->pci_vendor, dev->pci_device, dev->pdev->class);
  592. dev_priv->wq = create_workqueue("nouveau");
  593. if (!dev_priv->wq)
  594. return -EINVAL;
  595. /* resource 0 is mmio regs */
  596. /* resource 1 is linear FB */
  597. /* resource 2 is RAMIN (mmio regs + 0x1000000) */
  598. /* resource 6 is bios */
  599. /* map the mmio regs */
  600. mmio_start_offs = pci_resource_start(dev->pdev, 0);
  601. dev_priv->mmio = ioremap(mmio_start_offs, 0x00800000);
  602. if (!dev_priv->mmio) {
  603. NV_ERROR(dev, "Unable to initialize the mmio mapping. "
  604. "Please report your setup to " DRIVER_EMAIL "\n");
  605. return -EINVAL;
  606. }
  607. NV_DEBUG(dev, "regs mapped ok at 0x%llx\n",
  608. (unsigned long long)mmio_start_offs);
  609. #ifdef __BIG_ENDIAN
  610. /* Put the card in BE mode if it's not */
  611. if (nv_rd32(dev, NV03_PMC_BOOT_1))
  612. nv_wr32(dev, NV03_PMC_BOOT_1, 0x00000001);
  613. DRM_MEMORYBARRIER();
  614. #endif
  615. /* Time to determine the card architecture */
  616. reg0 = nv_rd32(dev, NV03_PMC_BOOT_0);
  617. /* We're dealing with >=NV10 */
  618. if ((reg0 & 0x0f000000) > 0) {
  619. /* Bit 27-20 contain the architecture in hex */
  620. dev_priv->chipset = (reg0 & 0xff00000) >> 20;
  621. /* NV04 or NV05 */
  622. } else if ((reg0 & 0xff00fff0) == 0x20004000) {
  623. if (reg0 & 0x00f00000)
  624. dev_priv->chipset = 0x05;
  625. else
  626. dev_priv->chipset = 0x04;
  627. } else
  628. dev_priv->chipset = 0xff;
  629. switch (dev_priv->chipset & 0xf0) {
  630. case 0x00:
  631. case 0x10:
  632. case 0x20:
  633. case 0x30:
  634. dev_priv->card_type = dev_priv->chipset & 0xf0;
  635. break;
  636. case 0x40:
  637. case 0x60:
  638. dev_priv->card_type = NV_40;
  639. break;
  640. case 0x50:
  641. case 0x80:
  642. case 0x90:
  643. case 0xa0:
  644. dev_priv->card_type = NV_50;
  645. break;
  646. default:
  647. NV_INFO(dev, "Unsupported chipset 0x%08x\n", reg0);
  648. return -EINVAL;
  649. }
  650. NV_INFO(dev, "Detected an NV%2x generation card (0x%08x)\n",
  651. dev_priv->card_type, reg0);
  652. /* map larger RAMIN aperture on NV40 cards */
  653. dev_priv->ramin = NULL;
  654. if (dev_priv->card_type >= NV_40) {
  655. int ramin_bar = 2;
  656. if (pci_resource_len(dev->pdev, ramin_bar) == 0)
  657. ramin_bar = 3;
  658. dev_priv->ramin_size = pci_resource_len(dev->pdev, ramin_bar);
  659. dev_priv->ramin = ioremap(
  660. pci_resource_start(dev->pdev, ramin_bar),
  661. dev_priv->ramin_size);
  662. if (!dev_priv->ramin) {
  663. NV_ERROR(dev, "Failed to init RAMIN mapping, "
  664. "limited instance memory available\n");
  665. }
  666. }
  667. /* On older cards (or if the above failed), create a map covering
  668. * the BAR0 PRAMIN aperture */
  669. if (!dev_priv->ramin) {
  670. dev_priv->ramin_size = 1 * 1024 * 1024;
  671. dev_priv->ramin = ioremap(mmio_start_offs + NV_RAMIN,
  672. dev_priv->ramin_size);
  673. if (!dev_priv->ramin) {
  674. NV_ERROR(dev, "Failed to map BAR0 PRAMIN.\n");
  675. return -ENOMEM;
  676. }
  677. }
  678. nouveau_OF_copy_vbios_to_ramin(dev);
  679. /* Special flags */
  680. if (dev->pci_device == 0x01a0)
  681. dev_priv->flags |= NV_NFORCE;
  682. else if (dev->pci_device == 0x01f0)
  683. dev_priv->flags |= NV_NFORCE2;
  684. /* For kernel modesetting, init card now and bring up fbcon */
  685. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  686. int ret = nouveau_card_init(dev);
  687. if (ret)
  688. return ret;
  689. }
  690. return 0;
  691. }
  692. static void nouveau_close(struct drm_device *dev)
  693. {
  694. struct drm_nouveau_private *dev_priv = dev->dev_private;
  695. /* In the case of an error dev_priv may not be allocated yet */
  696. if (dev_priv)
  697. nouveau_card_takedown(dev);
  698. }
  699. /* KMS: we need mmio at load time, not when the first drm client opens. */
  700. void nouveau_lastclose(struct drm_device *dev)
  701. {
  702. if (drm_core_check_feature(dev, DRIVER_MODESET))
  703. return;
  704. nouveau_close(dev);
  705. }
  706. int nouveau_unload(struct drm_device *dev)
  707. {
  708. struct drm_nouveau_private *dev_priv = dev->dev_private;
  709. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  710. if (dev_priv->card_type >= NV_50)
  711. nv50_display_destroy(dev);
  712. else
  713. nv04_display_destroy(dev);
  714. nouveau_close(dev);
  715. }
  716. iounmap(dev_priv->mmio);
  717. iounmap(dev_priv->ramin);
  718. kfree(dev_priv);
  719. dev->dev_private = NULL;
  720. return 0;
  721. }
  722. int nouveau_ioctl_getparam(struct drm_device *dev, void *data,
  723. struct drm_file *file_priv)
  724. {
  725. struct drm_nouveau_private *dev_priv = dev->dev_private;
  726. struct drm_nouveau_getparam *getparam = data;
  727. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  728. switch (getparam->param) {
  729. case NOUVEAU_GETPARAM_CHIPSET_ID:
  730. getparam->value = dev_priv->chipset;
  731. break;
  732. case NOUVEAU_GETPARAM_PCI_VENDOR:
  733. getparam->value = dev->pci_vendor;
  734. break;
  735. case NOUVEAU_GETPARAM_PCI_DEVICE:
  736. getparam->value = dev->pci_device;
  737. break;
  738. case NOUVEAU_GETPARAM_BUS_TYPE:
  739. if (drm_device_is_agp(dev))
  740. getparam->value = NV_AGP;
  741. else if (drm_device_is_pcie(dev))
  742. getparam->value = NV_PCIE;
  743. else
  744. getparam->value = NV_PCI;
  745. break;
  746. case NOUVEAU_GETPARAM_FB_PHYSICAL:
  747. getparam->value = dev_priv->fb_phys;
  748. break;
  749. case NOUVEAU_GETPARAM_AGP_PHYSICAL:
  750. getparam->value = dev_priv->gart_info.aper_base;
  751. break;
  752. case NOUVEAU_GETPARAM_PCI_PHYSICAL:
  753. if (dev->sg) {
  754. getparam->value = (unsigned long)dev->sg->virtual;
  755. } else {
  756. NV_ERROR(dev, "Requested PCIGART address, "
  757. "while no PCIGART was created\n");
  758. return -EINVAL;
  759. }
  760. break;
  761. case NOUVEAU_GETPARAM_FB_SIZE:
  762. getparam->value = dev_priv->fb_available_size;
  763. break;
  764. case NOUVEAU_GETPARAM_AGP_SIZE:
  765. getparam->value = dev_priv->gart_info.aper_size;
  766. break;
  767. case NOUVEAU_GETPARAM_VM_VRAM_BASE:
  768. getparam->value = dev_priv->vm_vram_base;
  769. break;
  770. case NOUVEAU_GETPARAM_GRAPH_UNITS:
  771. /* NV40 and NV50 versions are quite different, but register
  772. * address is the same. User is supposed to know the card
  773. * family anyway... */
  774. if (dev_priv->chipset >= 0x40) {
  775. getparam->value = nv_rd32(dev, NV40_PMC_GRAPH_UNITS);
  776. break;
  777. }
  778. /* FALLTHRU */
  779. default:
  780. NV_ERROR(dev, "unknown parameter %lld\n", getparam->param);
  781. return -EINVAL;
  782. }
  783. return 0;
  784. }
  785. int
  786. nouveau_ioctl_setparam(struct drm_device *dev, void *data,
  787. struct drm_file *file_priv)
  788. {
  789. struct drm_nouveau_setparam *setparam = data;
  790. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  791. switch (setparam->param) {
  792. default:
  793. NV_ERROR(dev, "unknown parameter %lld\n", setparam->param);
  794. return -EINVAL;
  795. }
  796. return 0;
  797. }
  798. /* Wait until (value(reg) & mask) == val, up until timeout has hit */
  799. bool nouveau_wait_until(struct drm_device *dev, uint64_t timeout,
  800. uint32_t reg, uint32_t mask, uint32_t val)
  801. {
  802. struct drm_nouveau_private *dev_priv = dev->dev_private;
  803. struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
  804. uint64_t start = ptimer->read(dev);
  805. do {
  806. if ((nv_rd32(dev, reg) & mask) == val)
  807. return true;
  808. } while (ptimer->read(dev) - start < timeout);
  809. return false;
  810. }
  811. /* Waits for PGRAPH to go completely idle */
  812. bool nouveau_wait_for_idle(struct drm_device *dev)
  813. {
  814. if (!nv_wait(NV04_PGRAPH_STATUS, 0xffffffff, 0x00000000)) {
  815. NV_ERROR(dev, "PGRAPH idle timed out with status 0x%08x\n",
  816. nv_rd32(dev, NV04_PGRAPH_STATUS));
  817. return false;
  818. }
  819. return true;
  820. }