nouveau_object.c 24 KB

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