gtt.c 15 KB

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