gtt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Copyright (c) 2007, Intel Corporation.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * Authors: Thomas Hellstrom <thomas-at-tungstengraphics.com>
  19. * Alan Cox <alan@linux.intel.com>
  20. */
  21. #include <drm/drmP.h>
  22. #include <linux/shmem_fs.h>
  23. #include "psb_drv.h"
  24. /*
  25. * GTT resource allocator - manage page mappings in GTT space
  26. */
  27. /**
  28. * psb_gtt_mask_pte - generate GTT pte entry
  29. * @pfn: page number to encode
  30. * @type: type of memory in the GTT
  31. *
  32. * Set the GTT entry for the appropriate memory type.
  33. */
  34. static inline uint32_t psb_gtt_mask_pte(uint32_t pfn, int type)
  35. {
  36. uint32_t mask = PSB_PTE_VALID;
  37. /* Ensure we explode rather than put an invalid low mapping of
  38. a high mapping page into the gtt */
  39. BUG_ON(pfn & ~(0xFFFFFFFF >> PAGE_SHIFT));
  40. if (type & PSB_MMU_CACHED_MEMORY)
  41. mask |= PSB_PTE_CACHED;
  42. if (type & PSB_MMU_RO_MEMORY)
  43. mask |= PSB_PTE_RO;
  44. if (type & PSB_MMU_WO_MEMORY)
  45. mask |= PSB_PTE_WO;
  46. return (pfn << PAGE_SHIFT) | mask;
  47. }
  48. /**
  49. * psb_gtt_entry - find the GTT entries for a gtt_range
  50. * @dev: our DRM device
  51. * @r: our GTT range
  52. *
  53. * Given a gtt_range object return the GTT offset of the page table
  54. * entries for this gtt_range
  55. */
  56. static u32 *psb_gtt_entry(struct drm_device *dev, struct gtt_range *r)
  57. {
  58. struct drm_psb_private *dev_priv = dev->dev_private;
  59. unsigned long offset;
  60. offset = r->resource.start - dev_priv->gtt_mem->start;
  61. return dev_priv->gtt_map + (offset >> PAGE_SHIFT);
  62. }
  63. /**
  64. * psb_gtt_insert - put an object into the GTT
  65. * @dev: our DRM device
  66. * @r: our GTT range
  67. *
  68. * Take our preallocated GTT range and insert the GEM object into
  69. * the GTT. This is protected via the gtt mutex which the caller
  70. * must hold.
  71. */
  72. static int psb_gtt_insert(struct drm_device *dev, struct gtt_range *r)
  73. {
  74. u32 *gtt_slot, pte;
  75. struct page **pages;
  76. int i;
  77. if (r->pages == NULL) {
  78. WARN_ON(1);
  79. return -EINVAL;
  80. }
  81. WARN_ON(r->stolen); /* refcount these maybe ? */
  82. gtt_slot = psb_gtt_entry(dev, r);
  83. pages = r->pages;
  84. /* Make sure changes are visible to the GPU */
  85. set_pages_array_wc(pages, r->npage);
  86. /* Write our page entries into the GTT itself */
  87. for (i = r->roll; i < r->npage; i++) {
  88. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  89. iowrite32(pte, gtt_slot++);
  90. }
  91. for (i = 0; i < r->roll; i++) {
  92. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  93. iowrite32(pte, gtt_slot++);
  94. }
  95. /* Make sure all the entries are set before we return */
  96. ioread32(gtt_slot - 1);
  97. return 0;
  98. }
  99. /**
  100. * psb_gtt_remove - remove an object from the GTT
  101. * @dev: our DRM device
  102. * @r: our GTT range
  103. *
  104. * Remove a preallocated GTT range from the GTT. Overwrite all the
  105. * page table entries with the dummy page. This is protected via the gtt
  106. * mutex which the caller must hold.
  107. */
  108. static void psb_gtt_remove(struct drm_device *dev, struct gtt_range *r)
  109. {
  110. struct drm_psb_private *dev_priv = dev->dev_private;
  111. u32 *gtt_slot, pte;
  112. int i;
  113. WARN_ON(r->stolen);
  114. gtt_slot = psb_gtt_entry(dev, r);
  115. pte = psb_gtt_mask_pte(page_to_pfn(dev_priv->scratch_page), 0);
  116. for (i = 0; i < r->npage; i++)
  117. iowrite32(pte, gtt_slot++);
  118. ioread32(gtt_slot - 1);
  119. set_pages_array_wb(r->pages, r->npage);
  120. }
  121. /**
  122. * psb_gtt_roll - set scrolling position
  123. * @dev: our DRM device
  124. * @r: the gtt mapping we are using
  125. * @roll: roll offset
  126. *
  127. * Roll an existing pinned mapping by moving the pages through the GTT.
  128. * This allows us to implement hardware scrolling on the consoles without
  129. * a 2D engine
  130. */
  131. void psb_gtt_roll(struct drm_device *dev, struct gtt_range *r, int roll)
  132. {
  133. u32 *gtt_slot, pte;
  134. int i;
  135. if (roll >= r->npage) {
  136. WARN_ON(1);
  137. return;
  138. }
  139. r->roll = roll;
  140. /* Not currently in the GTT - no worry we will write the mapping at
  141. the right position when it gets pinned */
  142. if (!r->stolen && !r->in_gart)
  143. return;
  144. gtt_slot = psb_gtt_entry(dev, r);
  145. for (i = r->roll; i < r->npage; i++) {
  146. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  147. iowrite32(pte, gtt_slot++);
  148. }
  149. for (i = 0; i < r->roll; i++) {
  150. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  151. iowrite32(pte, gtt_slot++);
  152. }
  153. ioread32(gtt_slot - 1);
  154. }
  155. /**
  156. * psb_gtt_attach_pages - attach and pin GEM pages
  157. * @gt: the gtt range
  158. *
  159. * Pin and build an in kernel list of the pages that back our GEM object.
  160. * While we hold this the pages cannot be swapped out. This is protected
  161. * via the gtt mutex which the caller must hold.
  162. */
  163. static int psb_gtt_attach_pages(struct gtt_range *gt)
  164. {
  165. struct inode *inode;
  166. struct address_space *mapping;
  167. int i;
  168. struct page *p;
  169. int pages = gt->gem.size / PAGE_SIZE;
  170. WARN_ON(gt->pages);
  171. /* This is the shared memory object that backs the GEM resource */
  172. inode = gt->gem.filp->f_path.dentry->d_inode;
  173. mapping = inode->i_mapping;
  174. gt->pages = kmalloc(pages * sizeof(struct page *), GFP_KERNEL);
  175. if (gt->pages == NULL)
  176. return -ENOMEM;
  177. gt->npage = pages;
  178. for (i = 0; i < pages; i++) {
  179. p = shmem_read_mapping_page(mapping, i);
  180. if (IS_ERR(p))
  181. goto err;
  182. gt->pages[i] = p;
  183. }
  184. return 0;
  185. err:
  186. while (i--)
  187. page_cache_release(gt->pages[i]);
  188. kfree(gt->pages);
  189. gt->pages = NULL;
  190. return PTR_ERR(p);
  191. }
  192. /**
  193. * psb_gtt_detach_pages - attach and pin GEM pages
  194. * @gt: the gtt range
  195. *
  196. * Undo the effect of psb_gtt_attach_pages. At this point the pages
  197. * must have been removed from the GTT as they could now be paged out
  198. * and move bus address. This is protected via the gtt mutex which the
  199. * caller must hold.
  200. */
  201. static void psb_gtt_detach_pages(struct gtt_range *gt)
  202. {
  203. int i;
  204. for (i = 0; i < gt->npage; i++) {
  205. /* FIXME: do we need to force dirty */
  206. set_page_dirty(gt->pages[i]);
  207. page_cache_release(gt->pages[i]);
  208. }
  209. kfree(gt->pages);
  210. gt->pages = NULL;
  211. }
  212. /**
  213. * psb_gtt_pin - pin pages into the GTT
  214. * @gt: range to pin
  215. *
  216. * Pin a set of pages into the GTT. The pins are refcounted so that
  217. * multiple pins need multiple unpins to undo.
  218. *
  219. * Non GEM backed objects treat this as a no-op as they are always GTT
  220. * backed objects.
  221. */
  222. int psb_gtt_pin(struct gtt_range *gt)
  223. {
  224. int ret = 0;
  225. struct drm_device *dev = gt->gem.dev;
  226. struct drm_psb_private *dev_priv = dev->dev_private;
  227. mutex_lock(&dev_priv->gtt_mutex);
  228. if (gt->in_gart == 0 && gt->stolen == 0) {
  229. ret = psb_gtt_attach_pages(gt);
  230. if (ret < 0)
  231. goto out;
  232. ret = psb_gtt_insert(dev, gt);
  233. if (ret < 0) {
  234. psb_gtt_detach_pages(gt);
  235. goto out;
  236. }
  237. }
  238. gt->in_gart++;
  239. out:
  240. mutex_unlock(&dev_priv->gtt_mutex);
  241. return ret;
  242. }
  243. /**
  244. * psb_gtt_unpin - Drop a GTT pin requirement
  245. * @gt: range to pin
  246. *
  247. * Undoes the effect of psb_gtt_pin. On the last drop the GEM object
  248. * will be removed from the GTT which will also drop the page references
  249. * and allow the VM to clean up or page stuff.
  250. *
  251. * Non GEM backed objects treat this as a no-op as they are always GTT
  252. * backed objects.
  253. */
  254. void psb_gtt_unpin(struct gtt_range *gt)
  255. {
  256. struct drm_device *dev = gt->gem.dev;
  257. struct drm_psb_private *dev_priv = dev->dev_private;
  258. mutex_lock(&dev_priv->gtt_mutex);
  259. WARN_ON(!gt->in_gart);
  260. gt->in_gart--;
  261. if (gt->in_gart == 0 && gt->stolen == 0) {
  262. psb_gtt_remove(dev, gt);
  263. psb_gtt_detach_pages(gt);
  264. }
  265. mutex_unlock(&dev_priv->gtt_mutex);
  266. }
  267. /*
  268. * GTT resource allocator - allocate and manage GTT address space
  269. */
  270. /**
  271. * psb_gtt_alloc_range - allocate GTT address space
  272. * @dev: Our DRM device
  273. * @len: length (bytes) of address space required
  274. * @name: resource name
  275. * @backed: resource should be backed by stolen pages
  276. *
  277. * Ask the kernel core to find us a suitable range of addresses
  278. * to use for a GTT mapping.
  279. *
  280. * Returns a gtt_range structure describing the object, or NULL on
  281. * error. On successful return the resource is both allocated and marked
  282. * as in use.
  283. */
  284. struct gtt_range *psb_gtt_alloc_range(struct drm_device *dev, int len,
  285. const char *name, int backed)
  286. {
  287. struct drm_psb_private *dev_priv = dev->dev_private;
  288. struct gtt_range *gt;
  289. struct resource *r = dev_priv->gtt_mem;
  290. int ret;
  291. unsigned long start, end;
  292. if (backed) {
  293. /* The start of the GTT is the stolen pages */
  294. start = r->start;
  295. end = r->start + dev_priv->gtt.stolen_size - 1;
  296. } else {
  297. /* The rest we will use for GEM backed objects */
  298. start = r->start + dev_priv->gtt.stolen_size;
  299. end = r->end;
  300. }
  301. gt = kzalloc(sizeof(struct gtt_range), GFP_KERNEL);
  302. if (gt == NULL)
  303. return NULL;
  304. gt->resource.name = name;
  305. gt->stolen = backed;
  306. gt->in_gart = backed;
  307. gt->roll = 0;
  308. /* Ensure this is set for non GEM objects */
  309. gt->gem.dev = dev;
  310. ret = allocate_resource(dev_priv->gtt_mem, &gt->resource,
  311. len, start, end, PAGE_SIZE, NULL, NULL);
  312. if (ret == 0) {
  313. gt->offset = gt->resource.start - r->start;
  314. return gt;
  315. }
  316. kfree(gt);
  317. return NULL;
  318. }
  319. /**
  320. * psb_gtt_free_range - release GTT address space
  321. * @dev: our DRM device
  322. * @gt: a mapping created with psb_gtt_alloc_range
  323. *
  324. * Release a resource that was allocated with psb_gtt_alloc_range. If the
  325. * object has been pinned by mmap users we clean this up here currently.
  326. */
  327. void psb_gtt_free_range(struct drm_device *dev, struct gtt_range *gt)
  328. {
  329. /* Undo the mmap pin if we are destroying the object */
  330. if (gt->mmapping) {
  331. psb_gtt_unpin(gt);
  332. gt->mmapping = 0;
  333. }
  334. WARN_ON(gt->in_gart && !gt->stolen);
  335. release_resource(&gt->resource);
  336. kfree(gt);
  337. }
  338. static void psb_gtt_alloc(struct drm_device *dev)
  339. {
  340. struct drm_psb_private *dev_priv = dev->dev_private;
  341. init_rwsem(&dev_priv->gtt.sem);
  342. }
  343. void psb_gtt_takedown(struct drm_device *dev)
  344. {
  345. struct drm_psb_private *dev_priv = dev->dev_private;
  346. if (dev_priv->gtt_map) {
  347. iounmap(dev_priv->gtt_map);
  348. dev_priv->gtt_map = NULL;
  349. }
  350. if (dev_priv->gtt_initialized) {
  351. pci_write_config_word(dev->pdev, PSB_GMCH_CTRL,
  352. dev_priv->gmch_ctrl);
  353. PSB_WVDC32(dev_priv->pge_ctl, PSB_PGETBL_CTL);
  354. (void) PSB_RVDC32(PSB_PGETBL_CTL);
  355. }
  356. if (dev_priv->vram_addr)
  357. iounmap(dev_priv->gtt_map);
  358. }
  359. int psb_gtt_init(struct drm_device *dev, int resume)
  360. {
  361. struct drm_psb_private *dev_priv = dev->dev_private;
  362. unsigned gtt_pages;
  363. unsigned long stolen_size, vram_stolen_size;
  364. unsigned i, num_pages;
  365. unsigned pfn_base;
  366. uint32_t dvmt_mode = 0;
  367. struct psb_gtt *pg;
  368. int ret = 0;
  369. uint32_t pte;
  370. mutex_init(&dev_priv->gtt_mutex);
  371. psb_gtt_alloc(dev);
  372. pg = &dev_priv->gtt;
  373. /* Enable the GTT */
  374. pci_read_config_word(dev->pdev, PSB_GMCH_CTRL, &dev_priv->gmch_ctrl);
  375. pci_write_config_word(dev->pdev, PSB_GMCH_CTRL,
  376. dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
  377. dev_priv->pge_ctl = PSB_RVDC32(PSB_PGETBL_CTL);
  378. PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
  379. (void) PSB_RVDC32(PSB_PGETBL_CTL);
  380. /* The root resource we allocate address space from */
  381. dev_priv->gtt_initialized = 1;
  382. pg->gtt_phys_start = dev_priv->pge_ctl & PAGE_MASK;
  383. /*
  384. * The video mmu has a hw bug when accessing 0x0D0000000.
  385. * Make gatt start at 0x0e000,0000. This doesn't actually
  386. * matter for us but may do if the video acceleration ever
  387. * gets opened up.
  388. */
  389. pg->mmu_gatt_start = 0xE0000000;
  390. pg->gtt_start = pci_resource_start(dev->pdev, PSB_GTT_RESOURCE);
  391. gtt_pages = pci_resource_len(dev->pdev, PSB_GTT_RESOURCE)
  392. >> PAGE_SHIFT;
  393. /* CDV doesn't report this. In which case the system has 64 gtt pages */
  394. if (pg->gtt_start == 0 || gtt_pages == 0) {
  395. dev_dbg(dev->dev, "GTT PCI BAR not initialized.\n");
  396. gtt_pages = 64;
  397. pg->gtt_start = dev_priv->pge_ctl;
  398. }
  399. pg->gatt_start = pci_resource_start(dev->pdev, PSB_GATT_RESOURCE);
  400. pg->gatt_pages = pci_resource_len(dev->pdev, PSB_GATT_RESOURCE)
  401. >> PAGE_SHIFT;
  402. dev_priv->gtt_mem = &dev->pdev->resource[PSB_GATT_RESOURCE];
  403. if (pg->gatt_pages == 0 || pg->gatt_start == 0) {
  404. static struct resource fudge; /* Preferably peppermint */
  405. /* This can occur on CDV systems. Fudge it in this case.
  406. We really don't care what imaginary space is being allocated
  407. at this point */
  408. dev_dbg(dev->dev, "GATT PCI BAR not initialized.\n");
  409. pg->gatt_start = 0x40000000;
  410. pg->gatt_pages = (128 * 1024 * 1024) >> PAGE_SHIFT;
  411. /* This is a little confusing but in fact the GTT is providing
  412. a view from the GPU into memory and not vice versa. As such
  413. this is really allocating space that is not the same as the
  414. CPU address space on CDV */
  415. fudge.start = 0x40000000;
  416. fudge.end = 0x40000000 + 128 * 1024 * 1024 - 1;
  417. fudge.name = "fudge";
  418. fudge.flags = IORESOURCE_MEM;
  419. dev_priv->gtt_mem = &fudge;
  420. }
  421. pci_read_config_dword(dev->pdev, PSB_BSM, &dev_priv->stolen_base);
  422. vram_stolen_size = pg->gtt_phys_start - dev_priv->stolen_base
  423. - PAGE_SIZE;
  424. stolen_size = vram_stolen_size;
  425. printk(KERN_INFO "Stolen memory information\n");
  426. printk(KERN_INFO " base in RAM: 0x%x\n", dev_priv->stolen_base);
  427. printk(KERN_INFO " size: %luK, calculated by (GTT RAM base) - (Stolen base), seems wrong\n",
  428. vram_stolen_size/1024);
  429. dvmt_mode = (dev_priv->gmch_ctrl >> 4) & 0x7;
  430. printk(KERN_INFO " the correct size should be: %dM(dvmt mode=%d)\n",
  431. (dvmt_mode == 1) ? 1 : (2 << (dvmt_mode - 1)), dvmt_mode);
  432. if (resume && (gtt_pages != pg->gtt_pages) &&
  433. (stolen_size != pg->stolen_size)) {
  434. dev_err(dev->dev, "GTT resume error.\n");
  435. ret = -EINVAL;
  436. goto out_err;
  437. }
  438. pg->gtt_pages = gtt_pages;
  439. pg->stolen_size = stolen_size;
  440. dev_priv->vram_stolen_size = vram_stolen_size;
  441. /*
  442. * Map the GTT and the stolen memory area
  443. */
  444. dev_priv->gtt_map = ioremap_nocache(pg->gtt_phys_start,
  445. gtt_pages << PAGE_SHIFT);
  446. if (!dev_priv->gtt_map) {
  447. dev_err(dev->dev, "Failure to map gtt.\n");
  448. ret = -ENOMEM;
  449. goto out_err;
  450. }
  451. dev_priv->vram_addr = ioremap_wc(dev_priv->stolen_base, stolen_size);
  452. if (!dev_priv->vram_addr) {
  453. dev_err(dev->dev, "Failure to map stolen base.\n");
  454. ret = -ENOMEM;
  455. goto out_err;
  456. }
  457. /*
  458. * Insert vram stolen pages into the GTT
  459. */
  460. pfn_base = dev_priv->stolen_base >> PAGE_SHIFT;
  461. num_pages = vram_stolen_size >> PAGE_SHIFT;
  462. printk(KERN_INFO"Set up %d stolen pages starting at 0x%08x, GTT offset %dK\n",
  463. num_pages, pfn_base << PAGE_SHIFT, 0);
  464. for (i = 0; i < num_pages; ++i) {
  465. pte = psb_gtt_mask_pte(pfn_base + i, 0);
  466. iowrite32(pte, dev_priv->gtt_map + i);
  467. }
  468. /*
  469. * Init rest of GTT to the scratch page to avoid accidents or scribbles
  470. */
  471. pfn_base = page_to_pfn(dev_priv->scratch_page);
  472. pte = psb_gtt_mask_pte(pfn_base, 0);
  473. for (; i < gtt_pages; ++i)
  474. iowrite32(pte, dev_priv->gtt_map + i);
  475. (void) ioread32(dev_priv->gtt_map + i - 1);
  476. return 0;
  477. out_err:
  478. psb_gtt_takedown(dev);
  479. return ret;
  480. }