nouveau_state.c 28 KB

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