nouveau_object.c 24 KB

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