nouveau_object.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /*
  2. * Copyright (C) 2006 Ben Skeggs.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial
  16. * portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. */
  27. /*
  28. * Authors:
  29. * Ben Skeggs <darktama@iinet.net.au>
  30. */
  31. #include "drmP.h"
  32. #include "drm.h"
  33. #include "nouveau_drv.h"
  34. #include "nouveau_drm.h"
  35. /* NVidia uses context objects to drive drawing operations.
  36. Context objects can be selected into 8 subchannels in the FIFO,
  37. and then used via DMA command buffers.
  38. A context object is referenced by a user defined handle (CARD32). The HW
  39. looks up graphics objects in a hash table in the instance RAM.
  40. An entry in the hash table consists of 2 CARD32. The first CARD32 contains
  41. the handle, the second one a bitfield, that contains the address of the
  42. object in instance RAM.
  43. The format of the second CARD32 seems to be:
  44. NV4 to NV30:
  45. 15: 0 instance_addr >> 4
  46. 17:16 engine (here uses 1 = graphics)
  47. 28:24 channel id (here uses 0)
  48. 31 valid (use 1)
  49. NV40:
  50. 15: 0 instance_addr >> 4 (maybe 19-0)
  51. 21:20 engine (here uses 1 = graphics)
  52. I'm unsure about the other bits, but using 0 seems to work.
  53. The key into the hash table depends on the object handle and channel id and
  54. is given as:
  55. */
  56. static uint32_t
  57. nouveau_ramht_hash_handle(struct drm_device *dev, int channel, uint32_t handle)
  58. {
  59. struct drm_nouveau_private *dev_priv = dev->dev_private;
  60. uint32_t hash = 0;
  61. int i;
  62. NV_DEBUG(dev, "ch%d handle=0x%08x\n", channel, handle);
  63. for (i = 32; i > 0; i -= dev_priv->ramht_bits) {
  64. hash ^= (handle & ((1 << dev_priv->ramht_bits) - 1));
  65. handle >>= dev_priv->ramht_bits;
  66. }
  67. if (dev_priv->card_type < NV_50)
  68. hash ^= channel << (dev_priv->ramht_bits - 4);
  69. hash <<= 3;
  70. NV_DEBUG(dev, "hash=0x%08x\n", hash);
  71. return hash;
  72. }
  73. static int
  74. nouveau_ramht_entry_valid(struct drm_device *dev, struct nouveau_gpuobj *ramht,
  75. uint32_t offset)
  76. {
  77. struct drm_nouveau_private *dev_priv = dev->dev_private;
  78. uint32_t ctx = nv_ro32(dev, ramht, (offset + 4)/4);
  79. if (dev_priv->card_type < NV_40)
  80. return ((ctx & NV_RAMHT_CONTEXT_VALID) != 0);
  81. return (ctx != 0);
  82. }
  83. static int
  84. nouveau_ramht_insert(struct drm_device *dev, struct nouveau_gpuobj_ref *ref)
  85. {
  86. struct drm_nouveau_private *dev_priv = dev->dev_private;
  87. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  88. struct nouveau_channel *chan = ref->channel;
  89. struct nouveau_gpuobj *ramht = chan->ramht ? chan->ramht->gpuobj : NULL;
  90. uint32_t ctx, co, ho;
  91. if (!ramht) {
  92. NV_ERROR(dev, "No hash table!\n");
  93. return -EINVAL;
  94. }
  95. if (dev_priv->card_type < NV_40) {
  96. ctx = NV_RAMHT_CONTEXT_VALID | (ref->instance >> 4) |
  97. (chan->id << NV_RAMHT_CONTEXT_CHANNEL_SHIFT) |
  98. (ref->gpuobj->engine << NV_RAMHT_CONTEXT_ENGINE_SHIFT);
  99. } else
  100. if (dev_priv->card_type < NV_50) {
  101. ctx = (ref->instance >> 4) |
  102. (chan->id << NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) |
  103. (ref->gpuobj->engine << NV40_RAMHT_CONTEXT_ENGINE_SHIFT);
  104. } else {
  105. if (ref->gpuobj->engine == NVOBJ_ENGINE_DISPLAY) {
  106. ctx = (ref->instance << 10) | 2;
  107. } else {
  108. ctx = (ref->instance >> 4) |
  109. ((ref->gpuobj->engine <<
  110. NV40_RAMHT_CONTEXT_ENGINE_SHIFT));
  111. }
  112. }
  113. instmem->prepare_access(dev, true);
  114. co = ho = nouveau_ramht_hash_handle(dev, chan->id, ref->handle);
  115. do {
  116. if (!nouveau_ramht_entry_valid(dev, ramht, co)) {
  117. NV_DEBUG(dev,
  118. "insert ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
  119. chan->id, co, ref->handle, ctx);
  120. nv_wo32(dev, ramht, (co + 0)/4, ref->handle);
  121. nv_wo32(dev, ramht, (co + 4)/4, ctx);
  122. list_add_tail(&ref->list, &chan->ramht_refs);
  123. instmem->finish_access(dev);
  124. return 0;
  125. }
  126. NV_DEBUG(dev, "collision ch%d 0x%08x: h=0x%08x\n",
  127. chan->id, co, nv_ro32(dev, ramht, co/4));
  128. co += 8;
  129. if (co >= dev_priv->ramht_size)
  130. co = 0;
  131. } while (co != ho);
  132. instmem->finish_access(dev);
  133. NV_ERROR(dev, "RAMHT space exhausted. ch=%d\n", chan->id);
  134. return -ENOMEM;
  135. }
  136. static void
  137. nouveau_ramht_remove(struct drm_device *dev, struct nouveau_gpuobj_ref *ref)
  138. {
  139. struct drm_nouveau_private *dev_priv = dev->dev_private;
  140. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  141. struct nouveau_channel *chan = ref->channel;
  142. struct nouveau_gpuobj *ramht = chan->ramht ? chan->ramht->gpuobj : NULL;
  143. uint32_t co, ho;
  144. if (!ramht) {
  145. NV_ERROR(dev, "No hash table!\n");
  146. return;
  147. }
  148. instmem->prepare_access(dev, true);
  149. co = ho = nouveau_ramht_hash_handle(dev, chan->id, ref->handle);
  150. do {
  151. if (nouveau_ramht_entry_valid(dev, ramht, co) &&
  152. (ref->handle == nv_ro32(dev, ramht, (co/4)))) {
  153. NV_DEBUG(dev,
  154. "remove ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
  155. chan->id, co, ref->handle,
  156. nv_ro32(dev, ramht, (co + 4)));
  157. nv_wo32(dev, ramht, (co + 0)/4, 0x00000000);
  158. nv_wo32(dev, ramht, (co + 4)/4, 0x00000000);
  159. list_del(&ref->list);
  160. instmem->finish_access(dev);
  161. return;
  162. }
  163. co += 8;
  164. if (co >= dev_priv->ramht_size)
  165. co = 0;
  166. } while (co != ho);
  167. list_del(&ref->list);
  168. instmem->finish_access(dev);
  169. NV_ERROR(dev, "RAMHT entry not found. ch=%d, handle=0x%08x\n",
  170. chan->id, ref->handle);
  171. }
  172. int
  173. nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
  174. uint32_t size, int align, uint32_t flags,
  175. struct nouveau_gpuobj **gpuobj_ret)
  176. {
  177. struct drm_nouveau_private *dev_priv = dev->dev_private;
  178. struct nouveau_engine *engine = &dev_priv->engine;
  179. struct nouveau_gpuobj *gpuobj;
  180. struct mem_block *pramin = NULL;
  181. int ret;
  182. NV_DEBUG(dev, "ch%d size=%u align=%d flags=0x%08x\n",
  183. chan ? chan->id : -1, size, align, flags);
  184. if (!dev_priv || !gpuobj_ret || *gpuobj_ret != NULL)
  185. return -EINVAL;
  186. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  187. if (!gpuobj)
  188. return -ENOMEM;
  189. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  190. gpuobj->flags = flags;
  191. gpuobj->im_channel = chan;
  192. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  193. /* Choose between global instmem heap, and per-channel private
  194. * instmem heap. On <NV50 allow requests for private instmem
  195. * to be satisfied from global heap if no per-channel area
  196. * available.
  197. */
  198. if (chan) {
  199. if (chan->ramin_heap) {
  200. NV_DEBUG(dev, "private heap\n");
  201. pramin = chan->ramin_heap;
  202. } else
  203. if (dev_priv->card_type < NV_50) {
  204. NV_DEBUG(dev, "global heap fallback\n");
  205. pramin = dev_priv->ramin_heap;
  206. }
  207. } else {
  208. NV_DEBUG(dev, "global heap\n");
  209. pramin = dev_priv->ramin_heap;
  210. }
  211. if (!pramin) {
  212. NV_ERROR(dev, "No PRAMIN heap!\n");
  213. return -EINVAL;
  214. }
  215. if (!chan) {
  216. ret = engine->instmem.populate(dev, gpuobj, &size);
  217. if (ret) {
  218. nouveau_gpuobj_del(dev, &gpuobj);
  219. return ret;
  220. }
  221. }
  222. /* Allocate a chunk of the PRAMIN aperture */
  223. gpuobj->im_pramin = nouveau_mem_alloc_block(pramin, size,
  224. drm_order(align),
  225. (struct drm_file *)-2, 0);
  226. if (!gpuobj->im_pramin) {
  227. nouveau_gpuobj_del(dev, &gpuobj);
  228. return -ENOMEM;
  229. }
  230. if (!chan) {
  231. ret = engine->instmem.bind(dev, gpuobj);
  232. if (ret) {
  233. nouveau_gpuobj_del(dev, &gpuobj);
  234. return ret;
  235. }
  236. }
  237. if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
  238. int i;
  239. engine->instmem.prepare_access(dev, true);
  240. for (i = 0; i < gpuobj->im_pramin->size; i += 4)
  241. nv_wo32(dev, gpuobj, i/4, 0);
  242. engine->instmem.finish_access(dev);
  243. }
  244. *gpuobj_ret = gpuobj;
  245. return 0;
  246. }
  247. int
  248. nouveau_gpuobj_early_init(struct drm_device *dev)
  249. {
  250. struct drm_nouveau_private *dev_priv = dev->dev_private;
  251. NV_DEBUG(dev, "\n");
  252. INIT_LIST_HEAD(&dev_priv->gpuobj_list);
  253. return 0;
  254. }
  255. int
  256. nouveau_gpuobj_init(struct drm_device *dev)
  257. {
  258. struct drm_nouveau_private *dev_priv = dev->dev_private;
  259. int ret;
  260. NV_DEBUG(dev, "\n");
  261. if (dev_priv->card_type < NV_50) {
  262. ret = nouveau_gpuobj_new_fake(dev,
  263. dev_priv->ramht_offset, ~0, dev_priv->ramht_size,
  264. NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ALLOW_NO_REFS,
  265. &dev_priv->ramht, NULL);
  266. if (ret)
  267. return ret;
  268. }
  269. return 0;
  270. }
  271. void
  272. nouveau_gpuobj_takedown(struct drm_device *dev)
  273. {
  274. struct drm_nouveau_private *dev_priv = dev->dev_private;
  275. NV_DEBUG(dev, "\n");
  276. nouveau_gpuobj_del(dev, &dev_priv->ramht);
  277. }
  278. void
  279. nouveau_gpuobj_late_takedown(struct drm_device *dev)
  280. {
  281. struct drm_nouveau_private *dev_priv = dev->dev_private;
  282. struct nouveau_gpuobj *gpuobj = NULL;
  283. struct list_head *entry, *tmp;
  284. NV_DEBUG(dev, "\n");
  285. list_for_each_safe(entry, tmp, &dev_priv->gpuobj_list) {
  286. gpuobj = list_entry(entry, struct nouveau_gpuobj, list);
  287. NV_ERROR(dev, "gpuobj %p still exists at takedown, refs=%d\n",
  288. gpuobj, gpuobj->refcount);
  289. gpuobj->refcount = 0;
  290. nouveau_gpuobj_del(dev, &gpuobj);
  291. }
  292. }
  293. int
  294. nouveau_gpuobj_del(struct drm_device *dev, struct nouveau_gpuobj **pgpuobj)
  295. {
  296. struct drm_nouveau_private *dev_priv = dev->dev_private;
  297. struct nouveau_engine *engine = &dev_priv->engine;
  298. struct nouveau_gpuobj *gpuobj;
  299. int i;
  300. NV_DEBUG(dev, "gpuobj %p\n", pgpuobj ? *pgpuobj : NULL);
  301. if (!dev_priv || !pgpuobj || !(*pgpuobj))
  302. return -EINVAL;
  303. gpuobj = *pgpuobj;
  304. if (gpuobj->refcount != 0) {
  305. NV_ERROR(dev, "gpuobj refcount is %d\n", gpuobj->refcount);
  306. return -EINVAL;
  307. }
  308. if (gpuobj->im_pramin && (gpuobj->flags & NVOBJ_FLAG_ZERO_FREE)) {
  309. engine->instmem.prepare_access(dev, true);
  310. for (i = 0; i < gpuobj->im_pramin->size; i += 4)
  311. nv_wo32(dev, gpuobj, i/4, 0);
  312. engine->instmem.finish_access(dev);
  313. }
  314. if (gpuobj->dtor)
  315. gpuobj->dtor(dev, gpuobj);
  316. if (gpuobj->im_backing && !(gpuobj->flags & NVOBJ_FLAG_FAKE))
  317. engine->instmem.clear(dev, gpuobj);
  318. if (gpuobj->im_pramin) {
  319. if (gpuobj->flags & NVOBJ_FLAG_FAKE)
  320. kfree(gpuobj->im_pramin);
  321. else
  322. nouveau_mem_free_block(gpuobj->im_pramin);
  323. }
  324. list_del(&gpuobj->list);
  325. *pgpuobj = NULL;
  326. kfree(gpuobj);
  327. return 0;
  328. }
  329. static int
  330. nouveau_gpuobj_instance_get(struct drm_device *dev,
  331. struct nouveau_channel *chan,
  332. struct nouveau_gpuobj *gpuobj, uint32_t *inst)
  333. {
  334. struct drm_nouveau_private *dev_priv = dev->dev_private;
  335. struct nouveau_gpuobj *cpramin;
  336. /* <NV50 use PRAMIN address everywhere */
  337. if (dev_priv->card_type < NV_50) {
  338. *inst = gpuobj->im_pramin->start;
  339. return 0;
  340. }
  341. if (chan && gpuobj->im_channel != chan) {
  342. NV_ERROR(dev, "Channel mismatch: obj %d, ref %d\n",
  343. gpuobj->im_channel->id, chan->id);
  344. return -EINVAL;
  345. }
  346. /* NV50 channel-local instance */
  347. if (chan) {
  348. cpramin = chan->ramin->gpuobj;
  349. *inst = gpuobj->im_pramin->start - cpramin->im_pramin->start;
  350. return 0;
  351. }
  352. /* NV50 global (VRAM) instance */
  353. if (!gpuobj->im_channel) {
  354. /* ...from global heap */
  355. if (!gpuobj->im_backing) {
  356. NV_ERROR(dev, "AII, no VRAM backing gpuobj\n");
  357. return -EINVAL;
  358. }
  359. *inst = gpuobj->im_backing_start;
  360. return 0;
  361. } else {
  362. /* ...from local heap */
  363. cpramin = gpuobj->im_channel->ramin->gpuobj;
  364. *inst = cpramin->im_backing_start +
  365. (gpuobj->im_pramin->start - cpramin->im_pramin->start);
  366. return 0;
  367. }
  368. return -EINVAL;
  369. }
  370. int
  371. nouveau_gpuobj_ref_add(struct drm_device *dev, struct nouveau_channel *chan,
  372. uint32_t handle, struct nouveau_gpuobj *gpuobj,
  373. struct nouveau_gpuobj_ref **ref_ret)
  374. {
  375. struct drm_nouveau_private *dev_priv = dev->dev_private;
  376. struct nouveau_gpuobj_ref *ref;
  377. uint32_t instance;
  378. int ret;
  379. NV_DEBUG(dev, "ch%d h=0x%08x gpuobj=%p\n",
  380. chan ? chan->id : -1, handle, gpuobj);
  381. if (!dev_priv || !gpuobj || (ref_ret && *ref_ret != NULL))
  382. return -EINVAL;
  383. if (!chan && !ref_ret)
  384. return -EINVAL;
  385. if (gpuobj->engine == NVOBJ_ENGINE_SW && !gpuobj->im_pramin) {
  386. /* sw object */
  387. instance = 0x40;
  388. } else {
  389. ret = nouveau_gpuobj_instance_get(dev, chan, gpuobj, &instance);
  390. if (ret)
  391. return ret;
  392. }
  393. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  394. if (!ref)
  395. return -ENOMEM;
  396. INIT_LIST_HEAD(&ref->list);
  397. ref->gpuobj = gpuobj;
  398. ref->channel = chan;
  399. ref->instance = instance;
  400. if (!ref_ret) {
  401. ref->handle = handle;
  402. ret = nouveau_ramht_insert(dev, ref);
  403. if (ret) {
  404. kfree(ref);
  405. return ret;
  406. }
  407. } else {
  408. ref->handle = ~0;
  409. *ref_ret = ref;
  410. }
  411. ref->gpuobj->refcount++;
  412. return 0;
  413. }
  414. int nouveau_gpuobj_ref_del(struct drm_device *dev, struct nouveau_gpuobj_ref **pref)
  415. {
  416. struct nouveau_gpuobj_ref *ref;
  417. NV_DEBUG(dev, "ref %p\n", pref ? *pref : NULL);
  418. if (!dev || !pref || *pref == NULL)
  419. return -EINVAL;
  420. ref = *pref;
  421. if (ref->handle != ~0)
  422. nouveau_ramht_remove(dev, ref);
  423. if (ref->gpuobj) {
  424. ref->gpuobj->refcount--;
  425. if (ref->gpuobj->refcount == 0) {
  426. if (!(ref->gpuobj->flags & NVOBJ_FLAG_ALLOW_NO_REFS))
  427. nouveau_gpuobj_del(dev, &ref->gpuobj);
  428. }
  429. }
  430. *pref = NULL;
  431. kfree(ref);
  432. return 0;
  433. }
  434. int
  435. nouveau_gpuobj_new_ref(struct drm_device *dev,
  436. struct nouveau_channel *oc, struct nouveau_channel *rc,
  437. uint32_t handle, uint32_t size, int align,
  438. uint32_t flags, struct nouveau_gpuobj_ref **ref)
  439. {
  440. struct nouveau_gpuobj *gpuobj = NULL;
  441. int ret;
  442. ret = nouveau_gpuobj_new(dev, oc, size, align, flags, &gpuobj);
  443. if (ret)
  444. return ret;
  445. ret = nouveau_gpuobj_ref_add(dev, rc, handle, gpuobj, ref);
  446. if (ret) {
  447. nouveau_gpuobj_del(dev, &gpuobj);
  448. return ret;
  449. }
  450. return 0;
  451. }
  452. int
  453. nouveau_gpuobj_ref_find(struct nouveau_channel *chan, uint32_t handle,
  454. struct nouveau_gpuobj_ref **ref_ret)
  455. {
  456. struct nouveau_gpuobj_ref *ref;
  457. struct list_head *entry, *tmp;
  458. list_for_each_safe(entry, tmp, &chan->ramht_refs) {
  459. ref = list_entry(entry, struct nouveau_gpuobj_ref, list);
  460. if (ref->handle == handle) {
  461. if (ref_ret)
  462. *ref_ret = ref;
  463. return 0;
  464. }
  465. }
  466. return -EINVAL;
  467. }
  468. int
  469. nouveau_gpuobj_new_fake(struct drm_device *dev, uint32_t p_offset,
  470. uint32_t b_offset, uint32_t size,
  471. uint32_t flags, struct nouveau_gpuobj **pgpuobj,
  472. struct nouveau_gpuobj_ref **pref)
  473. {
  474. struct drm_nouveau_private *dev_priv = dev->dev_private;
  475. struct nouveau_gpuobj *gpuobj = NULL;
  476. int i;
  477. NV_DEBUG(dev,
  478. "p_offset=0x%08x b_offset=0x%08x size=0x%08x flags=0x%08x\n",
  479. p_offset, b_offset, size, flags);
  480. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  481. if (!gpuobj)
  482. return -ENOMEM;
  483. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  484. gpuobj->im_channel = NULL;
  485. gpuobj->flags = flags | NVOBJ_FLAG_FAKE;
  486. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  487. if (p_offset != ~0) {
  488. gpuobj->im_pramin = kzalloc(sizeof(struct mem_block),
  489. GFP_KERNEL);
  490. if (!gpuobj->im_pramin) {
  491. nouveau_gpuobj_del(dev, &gpuobj);
  492. return -ENOMEM;
  493. }
  494. gpuobj->im_pramin->start = p_offset;
  495. gpuobj->im_pramin->size = size;
  496. }
  497. if (b_offset != ~0) {
  498. gpuobj->im_backing = (struct nouveau_bo *)-1;
  499. gpuobj->im_backing_start = b_offset;
  500. }
  501. if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
  502. dev_priv->engine.instmem.prepare_access(dev, true);
  503. for (i = 0; i < gpuobj->im_pramin->size; i += 4)
  504. nv_wo32(dev, gpuobj, i/4, 0);
  505. dev_priv->engine.instmem.finish_access(dev);
  506. }
  507. if (pref) {
  508. i = nouveau_gpuobj_ref_add(dev, NULL, 0, gpuobj, pref);
  509. if (i) {
  510. nouveau_gpuobj_del(dev, &gpuobj);
  511. return i;
  512. }
  513. }
  514. if (pgpuobj)
  515. *pgpuobj = gpuobj;
  516. return 0;
  517. }
  518. static uint32_t
  519. nouveau_gpuobj_class_instmem_size(struct drm_device *dev, int class)
  520. {
  521. struct drm_nouveau_private *dev_priv = dev->dev_private;
  522. /*XXX: dodgy hack for now */
  523. if (dev_priv->card_type >= NV_50)
  524. return 24;
  525. if (dev_priv->card_type >= NV_40)
  526. return 32;
  527. return 16;
  528. }
  529. /*
  530. DMA objects are used to reference a piece of memory in the
  531. framebuffer, PCI or AGP address space. Each object is 16 bytes big
  532. and looks as follows:
  533. entry[0]
  534. 11:0 class (seems like I can always use 0 here)
  535. 12 page table present?
  536. 13 page entry linear?
  537. 15:14 access: 0 rw, 1 ro, 2 wo
  538. 17:16 target: 0 NV memory, 1 NV memory tiled, 2 PCI, 3 AGP
  539. 31:20 dma adjust (bits 0-11 of the address)
  540. entry[1]
  541. dma limit (size of transfer)
  542. entry[X]
  543. 1 0 readonly, 1 readwrite
  544. 31:12 dma frame address of the page (bits 12-31 of the address)
  545. entry[N]
  546. page table terminator, same value as the first pte, as does nvidia
  547. rivatv uses 0xffffffff
  548. Non linear page tables need a list of frame addresses afterwards,
  549. the rivatv project has some info on this.
  550. The method below creates a DMA object in instance RAM and returns a handle
  551. to it that can be used to set up context objects.
  552. */
  553. int
  554. nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class,
  555. uint64_t offset, uint64_t size, int access,
  556. int target, struct nouveau_gpuobj **gpuobj)
  557. {
  558. struct drm_device *dev = chan->dev;
  559. struct drm_nouveau_private *dev_priv = dev->dev_private;
  560. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  561. int ret;
  562. NV_DEBUG(dev, "ch%d class=0x%04x offset=0x%llx size=0x%llx\n",
  563. chan->id, class, offset, size);
  564. NV_DEBUG(dev, "access=%d target=%d\n", access, target);
  565. switch (target) {
  566. case NV_DMA_TARGET_AGP:
  567. offset += dev_priv->gart_info.aper_base;
  568. break;
  569. default:
  570. break;
  571. }
  572. ret = nouveau_gpuobj_new(dev, chan,
  573. nouveau_gpuobj_class_instmem_size(dev, class),
  574. 16, NVOBJ_FLAG_ZERO_ALLOC |
  575. NVOBJ_FLAG_ZERO_FREE, gpuobj);
  576. if (ret) {
  577. NV_ERROR(dev, "Error creating gpuobj: %d\n", ret);
  578. return ret;
  579. }
  580. instmem->prepare_access(dev, true);
  581. if (dev_priv->card_type < NV_50) {
  582. uint32_t frame, adjust, pte_flags = 0;
  583. if (access != NV_DMA_ACCESS_RO)
  584. pte_flags |= (1<<1);
  585. adjust = offset & 0x00000fff;
  586. frame = offset & ~0x00000fff;
  587. nv_wo32(dev, *gpuobj, 0, ((1<<12) | (1<<13) |
  588. (adjust << 20) |
  589. (access << 14) |
  590. (target << 16) |
  591. class));
  592. nv_wo32(dev, *gpuobj, 1, size - 1);
  593. nv_wo32(dev, *gpuobj, 2, frame | pte_flags);
  594. nv_wo32(dev, *gpuobj, 3, frame | pte_flags);
  595. } else {
  596. uint64_t limit = offset + size - 1;
  597. uint32_t flags0, flags5;
  598. if (target == NV_DMA_TARGET_VIDMEM) {
  599. flags0 = 0x00190000;
  600. flags5 = 0x00010000;
  601. } else {
  602. flags0 = 0x7fc00000;
  603. flags5 = 0x00080000;
  604. }
  605. nv_wo32(dev, *gpuobj, 0, flags0 | class);
  606. nv_wo32(dev, *gpuobj, 1, lower_32_bits(limit));
  607. nv_wo32(dev, *gpuobj, 2, lower_32_bits(offset));
  608. nv_wo32(dev, *gpuobj, 3, ((upper_32_bits(limit) & 0xff) << 24) |
  609. (upper_32_bits(offset) & 0xff));
  610. nv_wo32(dev, *gpuobj, 5, flags5);
  611. }
  612. instmem->finish_access(dev);
  613. (*gpuobj)->engine = NVOBJ_ENGINE_SW;
  614. (*gpuobj)->class = class;
  615. return 0;
  616. }
  617. int
  618. nouveau_gpuobj_gart_dma_new(struct nouveau_channel *chan,
  619. uint64_t offset, uint64_t size, int access,
  620. struct nouveau_gpuobj **gpuobj,
  621. uint32_t *o_ret)
  622. {
  623. struct drm_device *dev = chan->dev;
  624. struct drm_nouveau_private *dev_priv = dev->dev_private;
  625. int ret;
  626. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP ||
  627. (dev_priv->card_type >= NV_50 &&
  628. dev_priv->gart_info.type == NOUVEAU_GART_SGDMA)) {
  629. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  630. offset + dev_priv->vm_gart_base,
  631. size, access, NV_DMA_TARGET_AGP,
  632. gpuobj);
  633. if (o_ret)
  634. *o_ret = 0;
  635. } else
  636. if (dev_priv->gart_info.type == NOUVEAU_GART_SGDMA) {
  637. *gpuobj = dev_priv->gart_info.sg_ctxdma;
  638. if (offset & ~0xffffffffULL) {
  639. NV_ERROR(dev, "obj offset exceeds 32-bits\n");
  640. return -EINVAL;
  641. }
  642. if (o_ret)
  643. *o_ret = (uint32_t)offset;
  644. ret = (*gpuobj != NULL) ? 0 : -EINVAL;
  645. } else {
  646. NV_ERROR(dev, "Invalid GART type %d\n", dev_priv->gart_info.type);
  647. return -EINVAL;
  648. }
  649. return ret;
  650. }
  651. /* Context objects in the instance RAM have the following structure.
  652. * On NV40 they are 32 byte long, on NV30 and smaller 16 bytes.
  653. NV4 - NV30:
  654. entry[0]
  655. 11:0 class
  656. 12 chroma key enable
  657. 13 user clip enable
  658. 14 swizzle enable
  659. 17:15 patch config:
  660. scrcopy_and, rop_and, blend_and, scrcopy, srccopy_pre, blend_pre
  661. 18 synchronize enable
  662. 19 endian: 1 big, 0 little
  663. 21:20 dither mode
  664. 23 single step enable
  665. 24 patch status: 0 invalid, 1 valid
  666. 25 context_surface 0: 1 valid
  667. 26 context surface 1: 1 valid
  668. 27 context pattern: 1 valid
  669. 28 context rop: 1 valid
  670. 29,30 context beta, beta4
  671. entry[1]
  672. 7:0 mono format
  673. 15:8 color format
  674. 31:16 notify instance address
  675. entry[2]
  676. 15:0 dma 0 instance address
  677. 31:16 dma 1 instance address
  678. entry[3]
  679. dma method traps
  680. NV40:
  681. No idea what the exact format is. Here's what can be deducted:
  682. entry[0]:
  683. 11:0 class (maybe uses more bits here?)
  684. 17 user clip enable
  685. 21:19 patch config
  686. 25 patch status valid ?
  687. entry[1]:
  688. 15:0 DMA notifier (maybe 20:0)
  689. entry[2]:
  690. 15:0 DMA 0 instance (maybe 20:0)
  691. 24 big endian
  692. entry[3]:
  693. 15:0 DMA 1 instance (maybe 20:0)
  694. entry[4]:
  695. entry[5]:
  696. set to 0?
  697. */
  698. int
  699. nouveau_gpuobj_gr_new(struct nouveau_channel *chan, int class,
  700. struct nouveau_gpuobj **gpuobj)
  701. {
  702. struct drm_device *dev = chan->dev;
  703. struct drm_nouveau_private *dev_priv = dev->dev_private;
  704. int ret;
  705. NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
  706. ret = nouveau_gpuobj_new(dev, chan,
  707. nouveau_gpuobj_class_instmem_size(dev, class),
  708. 16,
  709. NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
  710. gpuobj);
  711. if (ret) {
  712. NV_ERROR(dev, "Error creating gpuobj: %d\n", ret);
  713. return ret;
  714. }
  715. dev_priv->engine.instmem.prepare_access(dev, true);
  716. if (dev_priv->card_type >= NV_50) {
  717. nv_wo32(dev, *gpuobj, 0, class);
  718. nv_wo32(dev, *gpuobj, 5, 0x00010000);
  719. } else {
  720. switch (class) {
  721. case NV_CLASS_NULL:
  722. nv_wo32(dev, *gpuobj, 0, 0x00001030);
  723. nv_wo32(dev, *gpuobj, 1, 0xFFFFFFFF);
  724. break;
  725. default:
  726. if (dev_priv->card_type >= NV_40) {
  727. nv_wo32(dev, *gpuobj, 0, class);
  728. #ifdef __BIG_ENDIAN
  729. nv_wo32(dev, *gpuobj, 2, 0x01000000);
  730. #endif
  731. } else {
  732. #ifdef __BIG_ENDIAN
  733. nv_wo32(dev, *gpuobj, 0, class | 0x00080000);
  734. #else
  735. nv_wo32(dev, *gpuobj, 0, class);
  736. #endif
  737. }
  738. }
  739. }
  740. dev_priv->engine.instmem.finish_access(dev);
  741. (*gpuobj)->engine = NVOBJ_ENGINE_GR;
  742. (*gpuobj)->class = class;
  743. return 0;
  744. }
  745. int
  746. nouveau_gpuobj_sw_new(struct nouveau_channel *chan, int class,
  747. struct nouveau_gpuobj **gpuobj_ret)
  748. {
  749. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  750. struct nouveau_gpuobj *gpuobj;
  751. if (!chan || !gpuobj_ret || *gpuobj_ret != NULL)
  752. return -EINVAL;
  753. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  754. if (!gpuobj)
  755. return -ENOMEM;
  756. gpuobj->engine = NVOBJ_ENGINE_SW;
  757. gpuobj->class = class;
  758. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  759. *gpuobj_ret = gpuobj;
  760. return 0;
  761. }
  762. static int
  763. nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
  764. {
  765. struct drm_device *dev = chan->dev;
  766. struct drm_nouveau_private *dev_priv = dev->dev_private;
  767. struct nouveau_gpuobj *pramin = NULL;
  768. uint32_t size;
  769. uint32_t base;
  770. int ret;
  771. NV_DEBUG(dev, "ch%d\n", chan->id);
  772. /* Base amount for object storage (4KiB enough?) */
  773. size = 0x1000;
  774. base = 0;
  775. /* PGRAPH context */
  776. if (dev_priv->card_type == NV_50) {
  777. /* Various fixed table thingos */
  778. size += 0x1400; /* mostly unknown stuff */
  779. size += 0x4000; /* vm pd */
  780. base = 0x6000;
  781. /* RAMHT, not sure about setting size yet, 32KiB to be safe */
  782. size += 0x8000;
  783. /* RAMFC */
  784. size += 0x1000;
  785. /* PGRAPH context */
  786. size += 0x70000;
  787. }
  788. NV_DEBUG(dev, "ch%d PRAMIN size: 0x%08x bytes, base alloc=0x%08x\n",
  789. chan->id, size, base);
  790. ret = nouveau_gpuobj_new_ref(dev, NULL, NULL, 0, size, 0x1000, 0,
  791. &chan->ramin);
  792. if (ret) {
  793. NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
  794. return ret;
  795. }
  796. pramin = chan->ramin->gpuobj;
  797. ret = nouveau_mem_init_heap(&chan->ramin_heap,
  798. pramin->im_pramin->start + base, size);
  799. if (ret) {
  800. NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
  801. nouveau_gpuobj_ref_del(dev, &chan->ramin);
  802. return ret;
  803. }
  804. return 0;
  805. }
  806. int
  807. nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
  808. uint32_t vram_h, uint32_t tt_h)
  809. {
  810. struct drm_device *dev = chan->dev;
  811. struct drm_nouveau_private *dev_priv = dev->dev_private;
  812. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  813. struct nouveau_gpuobj *vram = NULL, *tt = NULL;
  814. int ret, i;
  815. INIT_LIST_HEAD(&chan->ramht_refs);
  816. NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
  817. /* Reserve a block of PRAMIN for the channel
  818. *XXX: maybe on <NV50 too at some point
  819. */
  820. if (0 || dev_priv->card_type == NV_50) {
  821. ret = nouveau_gpuobj_channel_init_pramin(chan);
  822. if (ret) {
  823. NV_ERROR(dev, "init pramin\n");
  824. return ret;
  825. }
  826. }
  827. /* NV50 VM
  828. * - Allocate per-channel page-directory
  829. * - Map GART and VRAM into the channel's address space at the
  830. * locations determined during init.
  831. */
  832. if (dev_priv->card_type >= NV_50) {
  833. uint32_t vm_offset, pde;
  834. instmem->prepare_access(dev, true);
  835. vm_offset = (dev_priv->chipset & 0xf0) == 0x50 ? 0x1400 : 0x200;
  836. vm_offset += chan->ramin->gpuobj->im_pramin->start;
  837. ret = nouveau_gpuobj_new_fake(dev, vm_offset, ~0, 0x4000,
  838. 0, &chan->vm_pd, NULL);
  839. if (ret) {
  840. instmem->finish_access(dev);
  841. return ret;
  842. }
  843. for (i = 0; i < 0x4000; i += 8) {
  844. nv_wo32(dev, chan->vm_pd, (i+0)/4, 0x00000000);
  845. nv_wo32(dev, chan->vm_pd, (i+4)/4, 0xdeadcafe);
  846. }
  847. pde = (dev_priv->vm_gart_base / (512*1024*1024)) * 2;
  848. ret = nouveau_gpuobj_ref_add(dev, NULL, 0,
  849. dev_priv->gart_info.sg_ctxdma,
  850. &chan->vm_gart_pt);
  851. if (ret) {
  852. instmem->finish_access(dev);
  853. return ret;
  854. }
  855. nv_wo32(dev, chan->vm_pd, pde++,
  856. chan->vm_gart_pt->instance | 0x03);
  857. nv_wo32(dev, chan->vm_pd, pde++, 0x00000000);
  858. pde = (dev_priv->vm_vram_base / (512*1024*1024)) * 2;
  859. for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
  860. ret = nouveau_gpuobj_ref_add(dev, NULL, 0,
  861. dev_priv->vm_vram_pt[i],
  862. &chan->vm_vram_pt[i]);
  863. if (ret) {
  864. instmem->finish_access(dev);
  865. return ret;
  866. }
  867. nv_wo32(dev, chan->vm_pd, pde++,
  868. chan->vm_vram_pt[i]->instance | 0x61);
  869. nv_wo32(dev, chan->vm_pd, pde++, 0x00000000);
  870. }
  871. instmem->finish_access(dev);
  872. }
  873. /* RAMHT */
  874. if (dev_priv->card_type < NV_50) {
  875. ret = nouveau_gpuobj_ref_add(dev, NULL, 0, dev_priv->ramht,
  876. &chan->ramht);
  877. if (ret)
  878. return ret;
  879. } else {
  880. ret = nouveau_gpuobj_new_ref(dev, chan, chan, 0,
  881. 0x8000, 16,
  882. NVOBJ_FLAG_ZERO_ALLOC,
  883. &chan->ramht);
  884. if (ret)
  885. return ret;
  886. }
  887. /* VRAM ctxdma */
  888. if (dev_priv->card_type >= NV_50) {
  889. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  890. 0, dev_priv->vm_end,
  891. NV_DMA_ACCESS_RW,
  892. NV_DMA_TARGET_AGP, &vram);
  893. if (ret) {
  894. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  895. return ret;
  896. }
  897. } else {
  898. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  899. 0, dev_priv->fb_available_size,
  900. NV_DMA_ACCESS_RW,
  901. NV_DMA_TARGET_VIDMEM, &vram);
  902. if (ret) {
  903. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  904. return ret;
  905. }
  906. }
  907. ret = nouveau_gpuobj_ref_add(dev, chan, vram_h, vram, NULL);
  908. if (ret) {
  909. NV_ERROR(dev, "Error referencing VRAM ctxdma: %d\n", ret);
  910. return ret;
  911. }
  912. /* TT memory ctxdma */
  913. if (dev_priv->card_type >= NV_50) {
  914. tt = vram;
  915. } else
  916. if (dev_priv->gart_info.type != NOUVEAU_GART_NONE) {
  917. ret = nouveau_gpuobj_gart_dma_new(chan, 0,
  918. dev_priv->gart_info.aper_size,
  919. NV_DMA_ACCESS_RW, &tt, NULL);
  920. } else {
  921. NV_ERROR(dev, "Invalid GART type %d\n", dev_priv->gart_info.type);
  922. ret = -EINVAL;
  923. }
  924. if (ret) {
  925. NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
  926. return ret;
  927. }
  928. ret = nouveau_gpuobj_ref_add(dev, chan, tt_h, tt, NULL);
  929. if (ret) {
  930. NV_ERROR(dev, "Error referencing TT ctxdma: %d\n", ret);
  931. return ret;
  932. }
  933. return 0;
  934. }
  935. void
  936. nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
  937. {
  938. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  939. struct drm_device *dev = chan->dev;
  940. struct list_head *entry, *tmp;
  941. struct nouveau_gpuobj_ref *ref;
  942. int i;
  943. NV_DEBUG(dev, "ch%d\n", chan->id);
  944. if (!chan->ramht_refs.next)
  945. return;
  946. list_for_each_safe(entry, tmp, &chan->ramht_refs) {
  947. ref = list_entry(entry, struct nouveau_gpuobj_ref, list);
  948. nouveau_gpuobj_ref_del(dev, &ref);
  949. }
  950. nouveau_gpuobj_ref_del(dev, &chan->ramht);
  951. nouveau_gpuobj_del(dev, &chan->vm_pd);
  952. nouveau_gpuobj_ref_del(dev, &chan->vm_gart_pt);
  953. for (i = 0; i < dev_priv->vm_vram_pt_nr; i++)
  954. nouveau_gpuobj_ref_del(dev, &chan->vm_vram_pt[i]);
  955. if (chan->ramin_heap)
  956. nouveau_mem_takedown(&chan->ramin_heap);
  957. if (chan->ramin)
  958. nouveau_gpuobj_ref_del(dev, &chan->ramin);
  959. }
  960. int
  961. nouveau_gpuobj_suspend(struct drm_device *dev)
  962. {
  963. struct drm_nouveau_private *dev_priv = dev->dev_private;
  964. struct nouveau_gpuobj *gpuobj;
  965. int i;
  966. if (dev_priv->card_type < NV_50) {
  967. dev_priv->susres.ramin_copy = vmalloc(dev_priv->ramin_rsvd_vram);
  968. if (!dev_priv->susres.ramin_copy)
  969. return -ENOMEM;
  970. for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4)
  971. dev_priv->susres.ramin_copy[i/4] = nv_ri32(dev, i);
  972. return 0;
  973. }
  974. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  975. if (!gpuobj->im_backing || (gpuobj->flags & NVOBJ_FLAG_FAKE))
  976. continue;
  977. gpuobj->im_backing_suspend = vmalloc(gpuobj->im_pramin->size);
  978. if (!gpuobj->im_backing_suspend) {
  979. nouveau_gpuobj_resume(dev);
  980. return -ENOMEM;
  981. }
  982. dev_priv->engine.instmem.prepare_access(dev, false);
  983. for (i = 0; i < gpuobj->im_pramin->size / 4; i++)
  984. gpuobj->im_backing_suspend[i] = nv_ro32(dev, gpuobj, i);
  985. dev_priv->engine.instmem.finish_access(dev);
  986. }
  987. return 0;
  988. }
  989. void
  990. nouveau_gpuobj_suspend_cleanup(struct drm_device *dev)
  991. {
  992. struct drm_nouveau_private *dev_priv = dev->dev_private;
  993. struct nouveau_gpuobj *gpuobj;
  994. if (dev_priv->card_type < NV_50) {
  995. vfree(dev_priv->susres.ramin_copy);
  996. dev_priv->susres.ramin_copy = NULL;
  997. return;
  998. }
  999. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  1000. if (!gpuobj->im_backing_suspend)
  1001. continue;
  1002. vfree(gpuobj->im_backing_suspend);
  1003. gpuobj->im_backing_suspend = NULL;
  1004. }
  1005. }
  1006. void
  1007. nouveau_gpuobj_resume(struct drm_device *dev)
  1008. {
  1009. struct drm_nouveau_private *dev_priv = dev->dev_private;
  1010. struct nouveau_gpuobj *gpuobj;
  1011. int i;
  1012. if (dev_priv->card_type < NV_50) {
  1013. for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4)
  1014. nv_wi32(dev, i, dev_priv->susres.ramin_copy[i/4]);
  1015. nouveau_gpuobj_suspend_cleanup(dev);
  1016. return;
  1017. }
  1018. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  1019. if (!gpuobj->im_backing_suspend)
  1020. continue;
  1021. dev_priv->engine.instmem.prepare_access(dev, true);
  1022. for (i = 0; i < gpuobj->im_pramin->size / 4; i++)
  1023. nv_wo32(dev, gpuobj, i, gpuobj->im_backing_suspend[i]);
  1024. dev_priv->engine.instmem.finish_access(dev);
  1025. }
  1026. nouveau_gpuobj_suspend_cleanup(dev);
  1027. }
  1028. int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
  1029. struct drm_file *file_priv)
  1030. {
  1031. struct drm_nouveau_private *dev_priv = dev->dev_private;
  1032. struct drm_nouveau_grobj_alloc *init = data;
  1033. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  1034. struct nouveau_pgraph_object_class *grc;
  1035. struct nouveau_gpuobj *gr = NULL;
  1036. struct nouveau_channel *chan;
  1037. int ret;
  1038. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  1039. NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(init->channel, file_priv, chan);
  1040. if (init->handle == ~0)
  1041. return -EINVAL;
  1042. grc = pgraph->grclass;
  1043. while (grc->id) {
  1044. if (grc->id == init->class)
  1045. break;
  1046. grc++;
  1047. }
  1048. if (!grc->id) {
  1049. NV_ERROR(dev, "Illegal object class: 0x%x\n", init->class);
  1050. return -EPERM;
  1051. }
  1052. if (nouveau_gpuobj_ref_find(chan, init->handle, NULL) == 0)
  1053. return -EEXIST;
  1054. if (!grc->software)
  1055. ret = nouveau_gpuobj_gr_new(chan, grc->id, &gr);
  1056. else
  1057. ret = nouveau_gpuobj_sw_new(chan, grc->id, &gr);
  1058. if (ret) {
  1059. NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
  1060. ret, init->channel, init->handle);
  1061. return ret;
  1062. }
  1063. ret = nouveau_gpuobj_ref_add(dev, chan, init->handle, gr, NULL);
  1064. if (ret) {
  1065. NV_ERROR(dev, "Error referencing object: %d (%d/0x%08x)\n",
  1066. ret, init->channel, init->handle);
  1067. nouveau_gpuobj_del(dev, &gr);
  1068. return ret;
  1069. }
  1070. return 0;
  1071. }
  1072. int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
  1073. struct drm_file *file_priv)
  1074. {
  1075. struct drm_nouveau_gpuobj_free *objfree = data;
  1076. struct nouveau_gpuobj_ref *ref;
  1077. struct nouveau_channel *chan;
  1078. int ret;
  1079. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  1080. NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(objfree->channel, file_priv, chan);
  1081. ret = nouveau_gpuobj_ref_find(chan, objfree->handle, &ref);
  1082. if (ret)
  1083. return ret;
  1084. nouveau_gpuobj_ref_del(dev, &ref);
  1085. return 0;
  1086. }