nouveau_object.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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 drm_mm *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.ml_entry.next) {
  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 = drm_mm_search_free(pramin, size, align, 0);
  224. if (gpuobj->im_pramin)
  225. gpuobj->im_pramin = drm_mm_get_block(gpuobj->im_pramin, size, align);
  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. drm_mm_put_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 drm_mm_node),
  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;
  750. struct nouveau_gpuobj *gpuobj;
  751. if (!chan || !gpuobj_ret || *gpuobj_ret != NULL)
  752. return -EINVAL;
  753. dev_priv = chan->dev->dev_private;
  754. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  755. if (!gpuobj)
  756. return -ENOMEM;
  757. gpuobj->engine = NVOBJ_ENGINE_SW;
  758. gpuobj->class = class;
  759. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  760. *gpuobj_ret = gpuobj;
  761. return 0;
  762. }
  763. static int
  764. nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
  765. {
  766. struct drm_device *dev = chan->dev;
  767. struct drm_nouveau_private *dev_priv = dev->dev_private;
  768. struct nouveau_gpuobj *pramin = NULL;
  769. uint32_t size;
  770. uint32_t base;
  771. int ret;
  772. NV_DEBUG(dev, "ch%d\n", chan->id);
  773. /* Base amount for object storage (4KiB enough?) */
  774. size = 0x1000;
  775. base = 0;
  776. /* PGRAPH context */
  777. if (dev_priv->card_type == NV_50) {
  778. /* Various fixed table thingos */
  779. size += 0x1400; /* mostly unknown stuff */
  780. size += 0x4000; /* vm pd */
  781. base = 0x6000;
  782. /* RAMHT, not sure about setting size yet, 32KiB to be safe */
  783. size += 0x8000;
  784. /* RAMFC */
  785. size += 0x1000;
  786. /* PGRAPH context */
  787. size += 0x70000;
  788. }
  789. NV_DEBUG(dev, "ch%d PRAMIN size: 0x%08x bytes, base alloc=0x%08x\n",
  790. chan->id, size, base);
  791. ret = nouveau_gpuobj_new_ref(dev, NULL, NULL, 0, size, 0x1000, 0,
  792. &chan->ramin);
  793. if (ret) {
  794. NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
  795. return ret;
  796. }
  797. pramin = chan->ramin->gpuobj;
  798. ret = drm_mm_init(&chan->ramin_heap, 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.free_stack.next)
  956. drm_mm_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_GET_USER_CHANNEL_WITH_RETURN(init->channel, file_priv, chan);
  1039. if (init->handle == ~0)
  1040. return -EINVAL;
  1041. grc = pgraph->grclass;
  1042. while (grc->id) {
  1043. if (grc->id == init->class)
  1044. break;
  1045. grc++;
  1046. }
  1047. if (!grc->id) {
  1048. NV_ERROR(dev, "Illegal object class: 0x%x\n", init->class);
  1049. return -EPERM;
  1050. }
  1051. if (nouveau_gpuobj_ref_find(chan, init->handle, NULL) == 0)
  1052. return -EEXIST;
  1053. if (!grc->software)
  1054. ret = nouveau_gpuobj_gr_new(chan, grc->id, &gr);
  1055. else
  1056. ret = nouveau_gpuobj_sw_new(chan, grc->id, &gr);
  1057. if (ret) {
  1058. NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
  1059. ret, init->channel, init->handle);
  1060. return ret;
  1061. }
  1062. ret = nouveau_gpuobj_ref_add(dev, chan, init->handle, gr, NULL);
  1063. if (ret) {
  1064. NV_ERROR(dev, "Error referencing object: %d (%d/0x%08x)\n",
  1065. ret, init->channel, init->handle);
  1066. nouveau_gpuobj_del(dev, &gr);
  1067. return ret;
  1068. }
  1069. return 0;
  1070. }
  1071. int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
  1072. struct drm_file *file_priv)
  1073. {
  1074. struct drm_nouveau_gpuobj_free *objfree = data;
  1075. struct nouveau_gpuobj_ref *ref;
  1076. struct nouveau_channel *chan;
  1077. int ret;
  1078. NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(objfree->channel, file_priv, chan);
  1079. ret = nouveau_gpuobj_ref_find(chan, objfree->handle, &ref);
  1080. if (ret)
  1081. return ret;
  1082. nouveau_gpuobj_ref_del(dev, &ref);
  1083. return 0;
  1084. }