nouveau_object.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. #include "nouveau_ramht.h"
  36. /* NVidia uses context objects to drive drawing operations.
  37. Context objects can be selected into 8 subchannels in the FIFO,
  38. and then used via DMA command buffers.
  39. A context object is referenced by a user defined handle (CARD32). The HW
  40. looks up graphics objects in a hash table in the instance RAM.
  41. An entry in the hash table consists of 2 CARD32. The first CARD32 contains
  42. the handle, the second one a bitfield, that contains the address of the
  43. object in instance RAM.
  44. The format of the second CARD32 seems to be:
  45. NV4 to NV30:
  46. 15: 0 instance_addr >> 4
  47. 17:16 engine (here uses 1 = graphics)
  48. 28:24 channel id (here uses 0)
  49. 31 valid (use 1)
  50. NV40:
  51. 15: 0 instance_addr >> 4 (maybe 19-0)
  52. 21:20 engine (here uses 1 = graphics)
  53. I'm unsure about the other bits, but using 0 seems to work.
  54. The key into the hash table depends on the object handle and channel id and
  55. is given as:
  56. */
  57. int
  58. nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
  59. uint32_t size, int align, uint32_t flags,
  60. struct nouveau_gpuobj **gpuobj_ret)
  61. {
  62. struct drm_nouveau_private *dev_priv = dev->dev_private;
  63. struct nouveau_engine *engine = &dev_priv->engine;
  64. struct nouveau_gpuobj *gpuobj;
  65. struct drm_mm_node *ramin = NULL;
  66. int ret;
  67. NV_DEBUG(dev, "ch%d size=%u align=%d flags=0x%08x\n",
  68. chan ? chan->id : -1, size, align, flags);
  69. if (!dev_priv || !gpuobj_ret || *gpuobj_ret != NULL)
  70. return -EINVAL;
  71. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  72. if (!gpuobj)
  73. return -ENOMEM;
  74. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  75. gpuobj->dev = dev;
  76. gpuobj->flags = flags;
  77. kref_init(&gpuobj->refcount);
  78. gpuobj->size = size;
  79. spin_lock(&dev_priv->ramin_lock);
  80. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  81. spin_unlock(&dev_priv->ramin_lock);
  82. if (chan) {
  83. NV_DEBUG(dev, "channel heap\n");
  84. ramin = drm_mm_search_free(&chan->ramin_heap, size, align, 0);
  85. if (ramin)
  86. ramin = drm_mm_get_block(ramin, size, align);
  87. if (!ramin) {
  88. nouveau_gpuobj_ref(NULL, &gpuobj);
  89. return -ENOMEM;
  90. }
  91. } else {
  92. NV_DEBUG(dev, "global heap\n");
  93. /* allocate backing pages, sets vinst */
  94. ret = engine->instmem.populate(dev, gpuobj, &size, align);
  95. if (ret) {
  96. nouveau_gpuobj_ref(NULL, &gpuobj);
  97. return ret;
  98. }
  99. /* try and get aperture space */
  100. do {
  101. if (drm_mm_pre_get(&dev_priv->ramin_heap))
  102. return -ENOMEM;
  103. spin_lock(&dev_priv->ramin_lock);
  104. ramin = drm_mm_search_free(&dev_priv->ramin_heap, size,
  105. align, 0);
  106. if (ramin == NULL) {
  107. spin_unlock(&dev_priv->ramin_lock);
  108. nouveau_gpuobj_ref(NULL, &gpuobj);
  109. return -ENOMEM;
  110. }
  111. ramin = drm_mm_get_block_atomic(ramin, size, align);
  112. spin_unlock(&dev_priv->ramin_lock);
  113. } while (ramin == NULL);
  114. /* on nv50 it's ok to fail, we have a fallback path */
  115. if (!ramin && dev_priv->card_type < NV_50) {
  116. nouveau_gpuobj_ref(NULL, &gpuobj);
  117. return -ENOMEM;
  118. }
  119. }
  120. /* if we got a chunk of the aperture, map pages into it */
  121. gpuobj->im_pramin = ramin;
  122. if (!chan && gpuobj->im_pramin && dev_priv->ramin_available) {
  123. ret = engine->instmem.bind(dev, gpuobj);
  124. if (ret) {
  125. nouveau_gpuobj_ref(NULL, &gpuobj);
  126. return ret;
  127. }
  128. }
  129. /* calculate the various different addresses for the object */
  130. if (chan) {
  131. gpuobj->pinst = chan->ramin->pinst;
  132. if (gpuobj->pinst != ~0)
  133. gpuobj->pinst += gpuobj->im_pramin->start;
  134. if (dev_priv->card_type < NV_50) {
  135. gpuobj->cinst = gpuobj->pinst;
  136. } else {
  137. gpuobj->cinst = gpuobj->im_pramin->start;
  138. gpuobj->vinst = gpuobj->im_pramin->start +
  139. chan->ramin->vinst;
  140. }
  141. } else {
  142. if (gpuobj->im_pramin)
  143. gpuobj->pinst = gpuobj->im_pramin->start;
  144. else
  145. gpuobj->pinst = ~0;
  146. gpuobj->cinst = 0xdeadbeef;
  147. }
  148. if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
  149. int i;
  150. for (i = 0; i < gpuobj->size; i += 4)
  151. nv_wo32(gpuobj, i, 0);
  152. engine->instmem.flush(dev);
  153. }
  154. *gpuobj_ret = gpuobj;
  155. return 0;
  156. }
  157. int
  158. nouveau_gpuobj_init(struct drm_device *dev)
  159. {
  160. struct drm_nouveau_private *dev_priv = dev->dev_private;
  161. NV_DEBUG(dev, "\n");
  162. INIT_LIST_HEAD(&dev_priv->gpuobj_list);
  163. spin_lock_init(&dev_priv->ramin_lock);
  164. dev_priv->ramin_base = ~0;
  165. return 0;
  166. }
  167. void
  168. nouveau_gpuobj_takedown(struct drm_device *dev)
  169. {
  170. struct drm_nouveau_private *dev_priv = dev->dev_private;
  171. NV_DEBUG(dev, "\n");
  172. BUG_ON(!list_empty(&dev_priv->gpuobj_list));
  173. }
  174. static void
  175. nouveau_gpuobj_del(struct kref *ref)
  176. {
  177. struct nouveau_gpuobj *gpuobj =
  178. container_of(ref, struct nouveau_gpuobj, refcount);
  179. struct drm_device *dev = gpuobj->dev;
  180. struct drm_nouveau_private *dev_priv = dev->dev_private;
  181. struct nouveau_engine *engine = &dev_priv->engine;
  182. int i;
  183. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  184. if (gpuobj->im_pramin && (gpuobj->flags & NVOBJ_FLAG_ZERO_FREE)) {
  185. for (i = 0; i < gpuobj->size; i += 4)
  186. nv_wo32(gpuobj, i, 0);
  187. engine->instmem.flush(dev);
  188. }
  189. if (gpuobj->dtor)
  190. gpuobj->dtor(dev, gpuobj);
  191. if (gpuobj->im_backing)
  192. engine->instmem.clear(dev, gpuobj);
  193. spin_lock(&dev_priv->ramin_lock);
  194. if (gpuobj->im_pramin)
  195. drm_mm_put_block(gpuobj->im_pramin);
  196. list_del(&gpuobj->list);
  197. spin_unlock(&dev_priv->ramin_lock);
  198. kfree(gpuobj);
  199. }
  200. void
  201. nouveau_gpuobj_ref(struct nouveau_gpuobj *ref, struct nouveau_gpuobj **ptr)
  202. {
  203. if (ref)
  204. kref_get(&ref->refcount);
  205. if (*ptr)
  206. kref_put(&(*ptr)->refcount, nouveau_gpuobj_del);
  207. *ptr = ref;
  208. }
  209. int
  210. nouveau_gpuobj_new_fake(struct drm_device *dev, u32 pinst, u64 vinst,
  211. u32 size, u32 flags, struct nouveau_gpuobj **pgpuobj)
  212. {
  213. struct drm_nouveau_private *dev_priv = dev->dev_private;
  214. struct nouveau_gpuobj *gpuobj = NULL;
  215. int i;
  216. NV_DEBUG(dev,
  217. "pinst=0x%08x vinst=0x%010llx size=0x%08x flags=0x%08x\n",
  218. pinst, vinst, size, flags);
  219. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  220. if (!gpuobj)
  221. return -ENOMEM;
  222. NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
  223. gpuobj->dev = dev;
  224. gpuobj->flags = flags;
  225. kref_init(&gpuobj->refcount);
  226. gpuobj->size = size;
  227. gpuobj->pinst = pinst;
  228. gpuobj->cinst = 0xdeadbeef;
  229. gpuobj->vinst = vinst;
  230. if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
  231. for (i = 0; i < gpuobj->size; i += 4)
  232. nv_wo32(gpuobj, i, 0);
  233. dev_priv->engine.instmem.flush(dev);
  234. }
  235. spin_lock(&dev_priv->ramin_lock);
  236. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  237. spin_unlock(&dev_priv->ramin_lock);
  238. *pgpuobj = gpuobj;
  239. return 0;
  240. }
  241. static uint32_t
  242. nouveau_gpuobj_class_instmem_size(struct drm_device *dev, int class)
  243. {
  244. struct drm_nouveau_private *dev_priv = dev->dev_private;
  245. /*XXX: dodgy hack for now */
  246. if (dev_priv->card_type >= NV_50)
  247. return 24;
  248. if (dev_priv->card_type >= NV_40)
  249. return 32;
  250. return 16;
  251. }
  252. /*
  253. DMA objects are used to reference a piece of memory in the
  254. framebuffer, PCI or AGP address space. Each object is 16 bytes big
  255. and looks as follows:
  256. entry[0]
  257. 11:0 class (seems like I can always use 0 here)
  258. 12 page table present?
  259. 13 page entry linear?
  260. 15:14 access: 0 rw, 1 ro, 2 wo
  261. 17:16 target: 0 NV memory, 1 NV memory tiled, 2 PCI, 3 AGP
  262. 31:20 dma adjust (bits 0-11 of the address)
  263. entry[1]
  264. dma limit (size of transfer)
  265. entry[X]
  266. 1 0 readonly, 1 readwrite
  267. 31:12 dma frame address of the page (bits 12-31 of the address)
  268. entry[N]
  269. page table terminator, same value as the first pte, as does nvidia
  270. rivatv uses 0xffffffff
  271. Non linear page tables need a list of frame addresses afterwards,
  272. the rivatv project has some info on this.
  273. The method below creates a DMA object in instance RAM and returns a handle
  274. to it that can be used to set up context objects.
  275. */
  276. int
  277. nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class,
  278. uint64_t offset, uint64_t size, int access,
  279. int target, struct nouveau_gpuobj **gpuobj)
  280. {
  281. struct drm_device *dev = chan->dev;
  282. struct drm_nouveau_private *dev_priv = dev->dev_private;
  283. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  284. int ret;
  285. NV_DEBUG(dev, "ch%d class=0x%04x offset=0x%llx size=0x%llx\n",
  286. chan->id, class, offset, size);
  287. NV_DEBUG(dev, "access=%d target=%d\n", access, target);
  288. switch (target) {
  289. case NV_DMA_TARGET_AGP:
  290. offset += dev_priv->gart_info.aper_base;
  291. break;
  292. default:
  293. break;
  294. }
  295. ret = nouveau_gpuobj_new(dev, chan,
  296. nouveau_gpuobj_class_instmem_size(dev, class),
  297. 16, NVOBJ_FLAG_ZERO_ALLOC |
  298. NVOBJ_FLAG_ZERO_FREE, gpuobj);
  299. if (ret) {
  300. NV_ERROR(dev, "Error creating gpuobj: %d\n", ret);
  301. return ret;
  302. }
  303. if (dev_priv->card_type < NV_50) {
  304. uint32_t frame, adjust, pte_flags = 0;
  305. if (access != NV_DMA_ACCESS_RO)
  306. pte_flags |= (1<<1);
  307. adjust = offset & 0x00000fff;
  308. frame = offset & ~0x00000fff;
  309. nv_wo32(*gpuobj, 0, ((1<<12) | (1<<13) | (adjust << 20) |
  310. (access << 14) | (target << 16) |
  311. class));
  312. nv_wo32(*gpuobj, 4, size - 1);
  313. nv_wo32(*gpuobj, 8, frame | pte_flags);
  314. nv_wo32(*gpuobj, 12, frame | pte_flags);
  315. } else {
  316. uint64_t limit = offset + size - 1;
  317. uint32_t flags0, flags5;
  318. if (target == NV_DMA_TARGET_VIDMEM) {
  319. flags0 = 0x00190000;
  320. flags5 = 0x00010000;
  321. } else {
  322. flags0 = 0x7fc00000;
  323. flags5 = 0x00080000;
  324. }
  325. nv_wo32(*gpuobj, 0, flags0 | class);
  326. nv_wo32(*gpuobj, 4, lower_32_bits(limit));
  327. nv_wo32(*gpuobj, 8, lower_32_bits(offset));
  328. nv_wo32(*gpuobj, 12, ((upper_32_bits(limit) & 0xff) << 24) |
  329. (upper_32_bits(offset) & 0xff));
  330. nv_wo32(*gpuobj, 20, flags5);
  331. }
  332. instmem->flush(dev);
  333. (*gpuobj)->engine = NVOBJ_ENGINE_SW;
  334. (*gpuobj)->class = class;
  335. return 0;
  336. }
  337. int
  338. nouveau_gpuobj_gart_dma_new(struct nouveau_channel *chan,
  339. uint64_t offset, uint64_t size, int access,
  340. struct nouveau_gpuobj **gpuobj,
  341. uint32_t *o_ret)
  342. {
  343. struct drm_device *dev = chan->dev;
  344. struct drm_nouveau_private *dev_priv = dev->dev_private;
  345. int ret;
  346. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP ||
  347. (dev_priv->card_type >= NV_50 &&
  348. dev_priv->gart_info.type == NOUVEAU_GART_SGDMA)) {
  349. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  350. offset + dev_priv->vm_gart_base,
  351. size, access, NV_DMA_TARGET_AGP,
  352. gpuobj);
  353. if (o_ret)
  354. *o_ret = 0;
  355. } else
  356. if (dev_priv->gart_info.type == NOUVEAU_GART_SGDMA) {
  357. nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma, gpuobj);
  358. if (offset & ~0xffffffffULL) {
  359. NV_ERROR(dev, "obj offset exceeds 32-bits\n");
  360. return -EINVAL;
  361. }
  362. if (o_ret)
  363. *o_ret = (uint32_t)offset;
  364. ret = (*gpuobj != NULL) ? 0 : -EINVAL;
  365. } else {
  366. NV_ERROR(dev, "Invalid GART type %d\n", dev_priv->gart_info.type);
  367. return -EINVAL;
  368. }
  369. return ret;
  370. }
  371. /* Context objects in the instance RAM have the following structure.
  372. * On NV40 they are 32 byte long, on NV30 and smaller 16 bytes.
  373. NV4 - NV30:
  374. entry[0]
  375. 11:0 class
  376. 12 chroma key enable
  377. 13 user clip enable
  378. 14 swizzle enable
  379. 17:15 patch config:
  380. scrcopy_and, rop_and, blend_and, scrcopy, srccopy_pre, blend_pre
  381. 18 synchronize enable
  382. 19 endian: 1 big, 0 little
  383. 21:20 dither mode
  384. 23 single step enable
  385. 24 patch status: 0 invalid, 1 valid
  386. 25 context_surface 0: 1 valid
  387. 26 context surface 1: 1 valid
  388. 27 context pattern: 1 valid
  389. 28 context rop: 1 valid
  390. 29,30 context beta, beta4
  391. entry[1]
  392. 7:0 mono format
  393. 15:8 color format
  394. 31:16 notify instance address
  395. entry[2]
  396. 15:0 dma 0 instance address
  397. 31:16 dma 1 instance address
  398. entry[3]
  399. dma method traps
  400. NV40:
  401. No idea what the exact format is. Here's what can be deducted:
  402. entry[0]:
  403. 11:0 class (maybe uses more bits here?)
  404. 17 user clip enable
  405. 21:19 patch config
  406. 25 patch status valid ?
  407. entry[1]:
  408. 15:0 DMA notifier (maybe 20:0)
  409. entry[2]:
  410. 15:0 DMA 0 instance (maybe 20:0)
  411. 24 big endian
  412. entry[3]:
  413. 15:0 DMA 1 instance (maybe 20:0)
  414. entry[4]:
  415. entry[5]:
  416. set to 0?
  417. */
  418. static int
  419. nouveau_gpuobj_sw_new(struct nouveau_channel *chan, int class,
  420. struct nouveau_gpuobj **gpuobj_ret)
  421. {
  422. struct drm_nouveau_private *dev_priv;
  423. struct nouveau_gpuobj *gpuobj;
  424. if (!chan || !gpuobj_ret || *gpuobj_ret != NULL)
  425. return -EINVAL;
  426. dev_priv = chan->dev->dev_private;
  427. gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
  428. if (!gpuobj)
  429. return -ENOMEM;
  430. gpuobj->dev = chan->dev;
  431. gpuobj->engine = NVOBJ_ENGINE_SW;
  432. gpuobj->class = class;
  433. kref_init(&gpuobj->refcount);
  434. gpuobj->cinst = 0x40;
  435. spin_lock(&dev_priv->ramin_lock);
  436. list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
  437. spin_unlock(&dev_priv->ramin_lock);
  438. *gpuobj_ret = gpuobj;
  439. return 0;
  440. }
  441. int
  442. nouveau_gpuobj_gr_new(struct nouveau_channel *chan, int class,
  443. struct nouveau_gpuobj **gpuobj)
  444. {
  445. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  446. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  447. struct nouveau_pgraph_object_class *grc;
  448. struct drm_device *dev = chan->dev;
  449. int ret;
  450. NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
  451. grc = pgraph->grclass;
  452. while (grc->id) {
  453. if (grc->id == class)
  454. break;
  455. grc++;
  456. }
  457. if (!grc->id) {
  458. NV_ERROR(dev, "illegal object class: 0x%x\n", class);
  459. return -EINVAL;
  460. }
  461. if (grc->engine == NVOBJ_ENGINE_SW)
  462. return nouveau_gpuobj_sw_new(chan, class, gpuobj);
  463. ret = nouveau_gpuobj_new(dev, chan,
  464. nouveau_gpuobj_class_instmem_size(dev, class),
  465. 16,
  466. NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
  467. gpuobj);
  468. if (ret) {
  469. NV_ERROR(dev, "error creating gpuobj: %d\n", ret);
  470. return ret;
  471. }
  472. if (dev_priv->card_type >= NV_50) {
  473. nv_wo32(*gpuobj, 0, class);
  474. nv_wo32(*gpuobj, 20, 0x00010000);
  475. } else {
  476. switch (class) {
  477. case NV_CLASS_NULL:
  478. nv_wo32(*gpuobj, 0, 0x00001030);
  479. nv_wo32(*gpuobj, 4, 0xFFFFFFFF);
  480. break;
  481. default:
  482. if (dev_priv->card_type >= NV_40) {
  483. nv_wo32(*gpuobj, 0, class);
  484. #ifdef __BIG_ENDIAN
  485. nv_wo32(*gpuobj, 8, 0x01000000);
  486. #endif
  487. } else {
  488. #ifdef __BIG_ENDIAN
  489. nv_wo32(*gpuobj, 0, class | 0x00080000);
  490. #else
  491. nv_wo32(*gpuobj, 0, class);
  492. #endif
  493. }
  494. }
  495. }
  496. dev_priv->engine.instmem.flush(dev);
  497. (*gpuobj)->engine = grc->engine;
  498. (*gpuobj)->class = class;
  499. return 0;
  500. }
  501. static int
  502. nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
  503. {
  504. struct drm_device *dev = chan->dev;
  505. struct drm_nouveau_private *dev_priv = dev->dev_private;
  506. uint32_t size;
  507. uint32_t base;
  508. int ret;
  509. NV_DEBUG(dev, "ch%d\n", chan->id);
  510. /* Base amount for object storage (4KiB enough?) */
  511. size = 0x1000;
  512. base = 0;
  513. /* PGRAPH context */
  514. size += dev_priv->engine.graph.grctx_size;
  515. if (dev_priv->card_type == NV_50) {
  516. /* Various fixed table thingos */
  517. size += 0x1400; /* mostly unknown stuff */
  518. size += 0x4000; /* vm pd */
  519. base = 0x6000;
  520. /* RAMHT, not sure about setting size yet, 32KiB to be safe */
  521. size += 0x8000;
  522. /* RAMFC */
  523. size += 0x1000;
  524. }
  525. ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
  526. if (ret) {
  527. NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
  528. return ret;
  529. }
  530. ret = drm_mm_init(&chan->ramin_heap, base, size);
  531. if (ret) {
  532. NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
  533. nouveau_gpuobj_ref(NULL, &chan->ramin);
  534. return ret;
  535. }
  536. return 0;
  537. }
  538. int
  539. nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
  540. uint32_t vram_h, uint32_t tt_h)
  541. {
  542. struct drm_device *dev = chan->dev;
  543. struct drm_nouveau_private *dev_priv = dev->dev_private;
  544. struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
  545. struct nouveau_gpuobj *vram = NULL, *tt = NULL;
  546. int ret, i;
  547. NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
  548. /* Allocate a chunk of memory for per-channel object storage */
  549. ret = nouveau_gpuobj_channel_init_pramin(chan);
  550. if (ret) {
  551. NV_ERROR(dev, "init pramin\n");
  552. return ret;
  553. }
  554. /* NV50 VM
  555. * - Allocate per-channel page-directory
  556. * - Map GART and VRAM into the channel's address space at the
  557. * locations determined during init.
  558. */
  559. if (dev_priv->card_type >= NV_50) {
  560. u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
  561. u64 vm_vinst = chan->ramin->vinst + pgd_offs;
  562. u32 vm_pinst = chan->ramin->pinst;
  563. u32 pde;
  564. if (vm_pinst != ~0)
  565. vm_pinst += pgd_offs;
  566. ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
  567. 0, &chan->vm_pd);
  568. if (ret)
  569. return ret;
  570. for (i = 0; i < 0x4000; i += 8) {
  571. nv_wo32(chan->vm_pd, i + 0, 0x00000000);
  572. nv_wo32(chan->vm_pd, i + 4, 0xdeadcafe);
  573. }
  574. nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma,
  575. &chan->vm_gart_pt);
  576. pde = (dev_priv->vm_gart_base / (512*1024*1024)) * 8;
  577. nv_wo32(chan->vm_pd, pde + 0, chan->vm_gart_pt->vinst | 3);
  578. nv_wo32(chan->vm_pd, pde + 4, 0x00000000);
  579. pde = (dev_priv->vm_vram_base / (512*1024*1024)) * 8;
  580. for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
  581. nouveau_gpuobj_ref(dev_priv->vm_vram_pt[i],
  582. &chan->vm_vram_pt[i]);
  583. nv_wo32(chan->vm_pd, pde + 0,
  584. chan->vm_vram_pt[i]->vinst | 0x61);
  585. nv_wo32(chan->vm_pd, pde + 4, 0x00000000);
  586. pde += 8;
  587. }
  588. instmem->flush(dev);
  589. }
  590. /* RAMHT */
  591. if (dev_priv->card_type < NV_50) {
  592. nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
  593. } else {
  594. struct nouveau_gpuobj *ramht = NULL;
  595. ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
  596. NVOBJ_FLAG_ZERO_ALLOC, &ramht);
  597. if (ret)
  598. return ret;
  599. ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
  600. nouveau_gpuobj_ref(NULL, &ramht);
  601. if (ret)
  602. return ret;
  603. }
  604. /* VRAM ctxdma */
  605. if (dev_priv->card_type >= NV_50) {
  606. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  607. 0, dev_priv->vm_end,
  608. NV_DMA_ACCESS_RW,
  609. NV_DMA_TARGET_AGP, &vram);
  610. if (ret) {
  611. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  612. return ret;
  613. }
  614. } else {
  615. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  616. 0, dev_priv->fb_available_size,
  617. NV_DMA_ACCESS_RW,
  618. NV_DMA_TARGET_VIDMEM, &vram);
  619. if (ret) {
  620. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  621. return ret;
  622. }
  623. }
  624. ret = nouveau_ramht_insert(chan, vram_h, vram);
  625. nouveau_gpuobj_ref(NULL, &vram);
  626. if (ret) {
  627. NV_ERROR(dev, "Error adding VRAM ctxdma to RAMHT: %d\n", ret);
  628. return ret;
  629. }
  630. /* TT memory ctxdma */
  631. if (dev_priv->card_type >= NV_50) {
  632. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  633. 0, dev_priv->vm_end,
  634. NV_DMA_ACCESS_RW,
  635. NV_DMA_TARGET_AGP, &tt);
  636. if (ret) {
  637. NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
  638. return ret;
  639. }
  640. } else
  641. if (dev_priv->gart_info.type != NOUVEAU_GART_NONE) {
  642. ret = nouveau_gpuobj_gart_dma_new(chan, 0,
  643. dev_priv->gart_info.aper_size,
  644. NV_DMA_ACCESS_RW, &tt, NULL);
  645. } else {
  646. NV_ERROR(dev, "Invalid GART type %d\n", dev_priv->gart_info.type);
  647. ret = -EINVAL;
  648. }
  649. if (ret) {
  650. NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
  651. return ret;
  652. }
  653. ret = nouveau_ramht_insert(chan, tt_h, tt);
  654. nouveau_gpuobj_ref(NULL, &tt);
  655. if (ret) {
  656. NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
  657. return ret;
  658. }
  659. return 0;
  660. }
  661. void
  662. nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
  663. {
  664. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  665. struct drm_device *dev = chan->dev;
  666. int i;
  667. NV_DEBUG(dev, "ch%d\n", chan->id);
  668. if (!chan->ramht)
  669. return;
  670. nouveau_ramht_ref(NULL, &chan->ramht, chan);
  671. nouveau_gpuobj_ref(NULL, &chan->vm_pd);
  672. nouveau_gpuobj_ref(NULL, &chan->vm_gart_pt);
  673. for (i = 0; i < dev_priv->vm_vram_pt_nr; i++)
  674. nouveau_gpuobj_ref(NULL, &chan->vm_vram_pt[i]);
  675. if (chan->ramin_heap.free_stack.next)
  676. drm_mm_takedown(&chan->ramin_heap);
  677. nouveau_gpuobj_ref(NULL, &chan->ramin);
  678. }
  679. int
  680. nouveau_gpuobj_suspend(struct drm_device *dev)
  681. {
  682. struct drm_nouveau_private *dev_priv = dev->dev_private;
  683. struct nouveau_gpuobj *gpuobj;
  684. int i;
  685. if (dev_priv->card_type < NV_50) {
  686. dev_priv->susres.ramin_copy = vmalloc(dev_priv->ramin_rsvd_vram);
  687. if (!dev_priv->susres.ramin_copy)
  688. return -ENOMEM;
  689. for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4)
  690. dev_priv->susres.ramin_copy[i/4] = nv_ri32(dev, i);
  691. return 0;
  692. }
  693. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  694. if (!gpuobj->im_backing)
  695. continue;
  696. gpuobj->im_backing_suspend = vmalloc(gpuobj->size);
  697. if (!gpuobj->im_backing_suspend) {
  698. nouveau_gpuobj_resume(dev);
  699. return -ENOMEM;
  700. }
  701. for (i = 0; i < gpuobj->size; i += 4)
  702. gpuobj->im_backing_suspend[i/4] = nv_ro32(gpuobj, i);
  703. }
  704. return 0;
  705. }
  706. void
  707. nouveau_gpuobj_suspend_cleanup(struct drm_device *dev)
  708. {
  709. struct drm_nouveau_private *dev_priv = dev->dev_private;
  710. struct nouveau_gpuobj *gpuobj;
  711. if (dev_priv->card_type < NV_50) {
  712. vfree(dev_priv->susres.ramin_copy);
  713. dev_priv->susres.ramin_copy = NULL;
  714. return;
  715. }
  716. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  717. if (!gpuobj->im_backing_suspend)
  718. continue;
  719. vfree(gpuobj->im_backing_suspend);
  720. gpuobj->im_backing_suspend = NULL;
  721. }
  722. }
  723. void
  724. nouveau_gpuobj_resume(struct drm_device *dev)
  725. {
  726. struct drm_nouveau_private *dev_priv = dev->dev_private;
  727. struct nouveau_gpuobj *gpuobj;
  728. int i;
  729. if (dev_priv->card_type < NV_50) {
  730. for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4)
  731. nv_wi32(dev, i, dev_priv->susres.ramin_copy[i/4]);
  732. nouveau_gpuobj_suspend_cleanup(dev);
  733. return;
  734. }
  735. list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
  736. if (!gpuobj->im_backing_suspend)
  737. continue;
  738. for (i = 0; i < gpuobj->size; i += 4)
  739. nv_wo32(gpuobj, i, gpuobj->im_backing_suspend[i/4]);
  740. dev_priv->engine.instmem.flush(dev);
  741. }
  742. nouveau_gpuobj_suspend_cleanup(dev);
  743. }
  744. int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
  745. struct drm_file *file_priv)
  746. {
  747. struct drm_nouveau_grobj_alloc *init = data;
  748. struct nouveau_gpuobj *gr = NULL;
  749. struct nouveau_channel *chan;
  750. int ret;
  751. if (init->handle == ~0)
  752. return -EINVAL;
  753. chan = nouveau_channel_get(dev, file_priv, init->channel);
  754. if (IS_ERR(chan))
  755. return PTR_ERR(chan);
  756. if (nouveau_ramht_find(chan, init->handle)) {
  757. ret = -EEXIST;
  758. goto out;
  759. }
  760. ret = nouveau_gpuobj_gr_new(chan, init->class, &gr);
  761. if (ret) {
  762. NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
  763. ret, init->channel, init->handle);
  764. goto out;
  765. }
  766. ret = nouveau_ramht_insert(chan, init->handle, gr);
  767. nouveau_gpuobj_ref(NULL, &gr);
  768. if (ret) {
  769. NV_ERROR(dev, "Error referencing object: %d (%d/0x%08x)\n",
  770. ret, init->channel, init->handle);
  771. }
  772. out:
  773. nouveau_channel_put(&chan);
  774. return ret;
  775. }
  776. int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
  777. struct drm_file *file_priv)
  778. {
  779. struct drm_nouveau_gpuobj_free *objfree = data;
  780. struct nouveau_channel *chan;
  781. int ret;
  782. chan = nouveau_channel_get(dev, file_priv, objfree->channel);
  783. if (IS_ERR(chan))
  784. return PTR_ERR(chan);
  785. ret = nouveau_ramht_remove(chan, objfree->handle);
  786. nouveau_channel_put(&chan);
  787. return ret;
  788. }
  789. u32
  790. nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
  791. {
  792. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  793. struct drm_device *dev = gpuobj->dev;
  794. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  795. u64 ptr = gpuobj->vinst + offset;
  796. u32 base = ptr >> 16;
  797. u32 val;
  798. spin_lock(&dev_priv->ramin_lock);
  799. if (dev_priv->ramin_base != base) {
  800. dev_priv->ramin_base = base;
  801. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  802. }
  803. val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
  804. spin_unlock(&dev_priv->ramin_lock);
  805. return val;
  806. }
  807. return nv_ri32(dev, gpuobj->pinst + offset);
  808. }
  809. void
  810. nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
  811. {
  812. struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
  813. struct drm_device *dev = gpuobj->dev;
  814. if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
  815. u64 ptr = gpuobj->vinst + offset;
  816. u32 base = ptr >> 16;
  817. spin_lock(&dev_priv->ramin_lock);
  818. if (dev_priv->ramin_base != base) {
  819. dev_priv->ramin_base = base;
  820. nv_wr32(dev, 0x001700, dev_priv->ramin_base);
  821. }
  822. nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
  823. spin_unlock(&dev_priv->ramin_lock);
  824. return;
  825. }
  826. nv_wi32(dev, gpuobj->pinst + offset, val);
  827. }