gtt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 "psb_drv.h"
  23. /*
  24. * GTT resource allocator - manage page mappings in GTT space
  25. */
  26. /**
  27. * psb_gtt_mask_pte - generate GTT pte entry
  28. * @pfn: page number to encode
  29. * @type: type of memory in the GTT
  30. *
  31. * Set the GTT entry for the appropriate memory type.
  32. */
  33. static inline uint32_t psb_gtt_mask_pte(uint32_t pfn, int type)
  34. {
  35. uint32_t mask = PSB_PTE_VALID;
  36. if (type & PSB_MMU_CACHED_MEMORY)
  37. mask |= PSB_PTE_CACHED;
  38. if (type & PSB_MMU_RO_MEMORY)
  39. mask |= PSB_PTE_RO;
  40. if (type & PSB_MMU_WO_MEMORY)
  41. mask |= PSB_PTE_WO;
  42. return (pfn << PAGE_SHIFT) | mask;
  43. }
  44. /**
  45. * psb_gtt_entry - find the GTT entries for a gtt_range
  46. * @dev: our DRM device
  47. * @r: our GTT range
  48. *
  49. * Given a gtt_range object return the GTT offset of the page table
  50. * entries for this gtt_range
  51. */
  52. u32 *psb_gtt_entry(struct drm_device *dev, struct gtt_range *r)
  53. {
  54. struct drm_psb_private *dev_priv = dev->dev_private;
  55. unsigned long offset;
  56. offset = r->resource.start - dev_priv->gtt_mem->start;
  57. return dev_priv->gtt_map + (offset >> PAGE_SHIFT);
  58. }
  59. /**
  60. * psb_gtt_insert - put an object into the GTT
  61. * @dev: our DRM device
  62. * @r: our GTT range
  63. *
  64. * Take our preallocated GTT range and insert the GEM object into
  65. * the GTT. This is protected via the gtt mutex which the caller
  66. * must hold.
  67. */
  68. static int psb_gtt_insert(struct drm_device *dev, struct gtt_range *r)
  69. {
  70. u32 *gtt_slot, pte;
  71. struct page **pages;
  72. int i;
  73. if (r->pages == NULL) {
  74. WARN_ON(1);
  75. return -EINVAL;
  76. }
  77. WARN_ON(r->stolen); /* refcount these maybe ? */
  78. gtt_slot = psb_gtt_entry(dev, r);
  79. pages = r->pages;
  80. /* Make sure changes are visible to the GPU */
  81. set_pages_array_uc(pages, r->npage);
  82. /* Write our page entries into the GTT itself */
  83. for (i = r->roll; i < r->npage; i++) {
  84. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  85. iowrite32(pte, gtt_slot++);
  86. }
  87. for (i = 0; i < r->roll; i++) {
  88. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  89. iowrite32(pte, gtt_slot++);
  90. }
  91. /* Make sure all the entries are set before we return */
  92. ioread32(gtt_slot - 1);
  93. return 0;
  94. }
  95. /**
  96. * psb_gtt_remove - remove an object from the GTT
  97. * @dev: our DRM device
  98. * @r: our GTT range
  99. *
  100. * Remove a preallocated GTT range from the GTT. Overwrite all the
  101. * page table entries with the dummy page. This is protected via the gtt
  102. * mutex which the caller must hold.
  103. */
  104. static void psb_gtt_remove(struct drm_device *dev, struct gtt_range *r)
  105. {
  106. struct drm_psb_private *dev_priv = dev->dev_private;
  107. u32 *gtt_slot, pte;
  108. int i;
  109. WARN_ON(r->stolen);
  110. gtt_slot = psb_gtt_entry(dev, r);
  111. pte = psb_gtt_mask_pte(page_to_pfn(dev_priv->scratch_page), 0);
  112. for (i = 0; i < r->npage; i++)
  113. iowrite32(pte, gtt_slot++);
  114. ioread32(gtt_slot - 1);
  115. set_pages_array_wb(r->pages, r->npage);
  116. }
  117. /**
  118. * psb_gtt_roll - set scrolling position
  119. * @dev: our DRM device
  120. * @r: the gtt mapping we are using
  121. * @roll: roll offset
  122. *
  123. * Roll an existing pinned mapping by moving the pages through the GTT.
  124. * This allows us to implement hardware scrolling on the consoles without
  125. * a 2D engine
  126. */
  127. void psb_gtt_roll(struct drm_device *dev, struct gtt_range *r, int roll)
  128. {
  129. u32 *gtt_slot, pte;
  130. int i;
  131. if (roll >= r->npage) {
  132. WARN_ON(1);
  133. return;
  134. }
  135. r->roll = roll;
  136. /* Not currently in the GTT - no worry we will write the mapping at
  137. the right position when it gets pinned */
  138. if (!r->stolen && !r->in_gart)
  139. return;
  140. gtt_slot = psb_gtt_entry(dev, r);
  141. for (i = r->roll; i < r->npage; i++) {
  142. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  143. iowrite32(pte, gtt_slot++);
  144. }
  145. for (i = 0; i < r->roll; i++) {
  146. pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
  147. iowrite32(pte, gtt_slot++);
  148. }
  149. ioread32(gtt_slot - 1);
  150. }
  151. /**
  152. * psb_gtt_attach_pages - attach and pin GEM pages
  153. * @gt: the gtt range
  154. *
  155. * Pin and build an in kernel list of the pages that back our GEM object.
  156. * While we hold this the pages cannot be swapped out. This is protected
  157. * via the gtt mutex which the caller must hold.
  158. */
  159. static int psb_gtt_attach_pages(struct gtt_range *gt)
  160. {
  161. struct inode *inode;
  162. struct address_space *mapping;
  163. int i;
  164. struct page *p;
  165. int pages = gt->gem.size / PAGE_SIZE;
  166. WARN_ON(gt->pages);
  167. /* This is the shared memory object that backs the GEM resource */
  168. inode = gt->gem.filp->f_path.dentry->d_inode;
  169. mapping = inode->i_mapping;
  170. gt->pages = kmalloc(pages * sizeof(struct page *), GFP_KERNEL);
  171. if (gt->pages == NULL)
  172. return -ENOMEM;
  173. gt->npage = pages;
  174. for (i = 0; i < pages; i++) {
  175. /* FIXME: needs updating as per mail from Hugh Dickins */
  176. p = read_cache_page_gfp(mapping, i,
  177. __GFP_COLD | GFP_KERNEL);
  178. if (IS_ERR(p))
  179. goto err;
  180. gt->pages[i] = p;
  181. }
  182. return 0;
  183. err:
  184. while (i--)
  185. page_cache_release(gt->pages[i]);
  186. kfree(gt->pages);
  187. gt->pages = NULL;
  188. return PTR_ERR(p);
  189. }
  190. /**
  191. * psb_gtt_detach_pages - attach and pin GEM pages
  192. * @gt: the gtt range
  193. *
  194. * Undo the effect of psb_gtt_attach_pages. At this point the pages
  195. * must have been removed from the GTT as they could now be paged out
  196. * and move bus address. This is protected via the gtt mutex which the
  197. * caller must hold.
  198. */
  199. static void psb_gtt_detach_pages(struct gtt_range *gt)
  200. {
  201. int i;
  202. for (i = 0; i < gt->npage; i++) {
  203. /* FIXME: do we need to force dirty */
  204. set_page_dirty(gt->pages[i]);
  205. page_cache_release(gt->pages[i]);
  206. }
  207. kfree(gt->pages);
  208. gt->pages = NULL;
  209. }
  210. /**
  211. * psb_gtt_pin - pin pages into the GTT
  212. * @gt: range to pin
  213. *
  214. * Pin a set of pages into the GTT. The pins are refcounted so that
  215. * multiple pins need multiple unpins to undo.
  216. *
  217. * Non GEM backed objects treat this as a no-op as they are always GTT
  218. * backed objects.
  219. */
  220. int psb_gtt_pin(struct gtt_range *gt)
  221. {
  222. int ret = 0;
  223. struct drm_device *dev = gt->gem.dev;
  224. struct drm_psb_private *dev_priv = dev->dev_private;
  225. mutex_lock(&dev_priv->gtt_mutex);
  226. if (gt->in_gart == 0 && gt->stolen == 0) {
  227. ret = psb_gtt_attach_pages(gt);
  228. if (ret < 0)
  229. goto out;
  230. ret = psb_gtt_insert(dev, gt);
  231. if (ret < 0) {
  232. psb_gtt_detach_pages(gt);
  233. goto out;
  234. }
  235. }
  236. gt->in_gart++;
  237. out:
  238. mutex_unlock(&dev_priv->gtt_mutex);
  239. return ret;
  240. }
  241. /**
  242. * psb_gtt_unpin - Drop a GTT pin requirement
  243. * @gt: range to pin
  244. *
  245. * Undoes the effect of psb_gtt_pin. On the last drop the GEM object
  246. * will be removed from the GTT which will also drop the page references
  247. * and allow the VM to clean up or page stuff.
  248. *
  249. * Non GEM backed objects treat this as a no-op as they are always GTT
  250. * backed objects.
  251. */
  252. void psb_gtt_unpin(struct gtt_range *gt)
  253. {
  254. struct drm_device *dev = gt->gem.dev;
  255. struct drm_psb_private *dev_priv = dev->dev_private;
  256. mutex_lock(&dev_priv->gtt_mutex);
  257. WARN_ON(!gt->in_gart);
  258. gt->in_gart--;
  259. if (gt->in_gart == 0 && gt->stolen == 0) {
  260. psb_gtt_remove(dev, gt);
  261. psb_gtt_detach_pages(gt);
  262. }
  263. mutex_unlock(&dev_priv->gtt_mutex);
  264. }
  265. /*
  266. * GTT resource allocator - allocate and manage GTT address space
  267. */
  268. /**
  269. * psb_gtt_alloc_range - allocate GTT address space
  270. * @dev: Our DRM device
  271. * @len: length (bytes) of address space required
  272. * @name: resource name
  273. * @backed: resource should be backed by stolen pages
  274. *
  275. * Ask the kernel core to find us a suitable range of addresses
  276. * to use for a GTT mapping.
  277. *
  278. * Returns a gtt_range structure describing the object, or NULL on
  279. * error. On successful return the resource is both allocated and marked
  280. * as in use.
  281. */
  282. struct gtt_range *psb_gtt_alloc_range(struct drm_device *dev, int len,
  283. const char *name, int backed)
  284. {
  285. struct drm_psb_private *dev_priv = dev->dev_private;
  286. struct gtt_range *gt;
  287. struct resource *r = dev_priv->gtt_mem;
  288. int ret;
  289. unsigned long start, end;
  290. if (backed) {
  291. /* The start of the GTT is the stolen pages */
  292. start = r->start;
  293. end = r->start + dev_priv->gtt.stolen_size - 1;
  294. } else {
  295. /* The rest we will use for GEM backed objects */
  296. start = r->start + dev_priv->gtt.stolen_size;
  297. end = r->end;
  298. }
  299. gt = kzalloc(sizeof(struct gtt_range), GFP_KERNEL);
  300. if (gt == NULL)
  301. return NULL;
  302. gt->resource.name = name;
  303. gt->stolen = backed;
  304. gt->in_gart = backed;
  305. gt->roll = 0;
  306. /* Ensure this is set for non GEM objects */
  307. gt->gem.dev = dev;
  308. ret = allocate_resource(dev_priv->gtt_mem, &gt->resource,
  309. len, start, end, PAGE_SIZE, NULL, NULL);
  310. if (ret == 0) {
  311. gt->offset = gt->resource.start - r->start;
  312. return gt;
  313. }
  314. kfree(gt);
  315. return NULL;
  316. }
  317. /**
  318. * psb_gtt_free_range - release GTT address space
  319. * @dev: our DRM device
  320. * @gt: a mapping created with psb_gtt_alloc_range
  321. *
  322. * Release a resource that was allocated with psb_gtt_alloc_range. If the
  323. * object has been pinned by mmap users we clean this up here currently.
  324. */
  325. void psb_gtt_free_range(struct drm_device *dev, struct gtt_range *gt)
  326. {
  327. /* Undo the mmap pin if we are destroying the object */
  328. if (gt->mmapping) {
  329. psb_gtt_unpin(gt);
  330. gt->mmapping = 0;
  331. }
  332. WARN_ON(gt->in_gart && !gt->stolen);
  333. release_resource(&gt->resource);
  334. kfree(gt);
  335. }
  336. void psb_gtt_alloc(struct drm_device *dev)
  337. {
  338. struct drm_psb_private *dev_priv = dev->dev_private;
  339. init_rwsem(&dev_priv->gtt.sem);
  340. }
  341. void psb_gtt_takedown(struct drm_device *dev)
  342. {
  343. struct drm_psb_private *dev_priv = dev->dev_private;
  344. if (dev_priv->gtt_map) {
  345. iounmap(dev_priv->gtt_map);
  346. dev_priv->gtt_map = NULL;
  347. }
  348. if (dev_priv->gtt_initialized) {
  349. pci_write_config_word(dev->pdev, PSB_GMCH_CTRL,
  350. dev_priv->gmch_ctrl);
  351. PSB_WVDC32(dev_priv->pge_ctl, PSB_PGETBL_CTL);
  352. (void) PSB_RVDC32(PSB_PGETBL_CTL);
  353. }
  354. if (dev_priv->vram_addr)
  355. iounmap(dev_priv->gtt_map);
  356. }
  357. int psb_gtt_init(struct drm_device *dev, int resume)
  358. {
  359. struct drm_psb_private *dev_priv = dev->dev_private;
  360. unsigned gtt_pages;
  361. unsigned long stolen_size, vram_stolen_size;
  362. unsigned i, num_pages;
  363. unsigned pfn_base;
  364. uint32_t vram_pages;
  365. uint32_t dvmt_mode = 0;
  366. struct psb_gtt *pg;
  367. int ret = 0;
  368. uint32_t pte;
  369. mutex_init(&dev_priv->gtt_mutex);
  370. psb_gtt_alloc(dev);
  371. pg = &dev_priv->gtt;
  372. /* Enable the GTT */
  373. pci_read_config_word(dev->pdev, PSB_GMCH_CTRL, &dev_priv->gmch_ctrl);
  374. pci_write_config_word(dev->pdev, PSB_GMCH_CTRL,
  375. dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
  376. dev_priv->pge_ctl = PSB_RVDC32(PSB_PGETBL_CTL);
  377. PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
  378. (void) PSB_RVDC32(PSB_PGETBL_CTL);
  379. /* The root resource we allocate address space from */
  380. dev_priv->gtt_initialized = 1;
  381. pg->gtt_phys_start = dev_priv->pge_ctl & PAGE_MASK;
  382. /*
  383. * The video mmu has a hw bug when accessing 0x0D0000000.
  384. * Make gatt start at 0x0e000,0000. This doesn't actually
  385. * matter for us but may do if the video acceleration ever
  386. * gets opened up.
  387. */
  388. pg->mmu_gatt_start = 0xE0000000;
  389. pg->gtt_start = pci_resource_start(dev->pdev, PSB_GTT_RESOURCE);
  390. gtt_pages = pci_resource_len(dev->pdev, PSB_GTT_RESOURCE)
  391. >> PAGE_SHIFT;
  392. /* Some CDV firmware doesn't report this currently. In which case the
  393. system has 64 gtt pages */
  394. if (pg->gtt_start == 0 || gtt_pages == 0) {
  395. dev_err(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 SDV systems. Fudge it in this case.
  406. We really don't care what imaginary space is being allocated
  407. at this point */
  408. dev_err(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. vram_pages = 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. }