/drivers/staging/gma500/psb_gtt.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 539 lines · 302 code · 78 blank · 159 comment · 46 complexity · 8fde704dc3b279cd1b0fe94d72b4e5e5 MD5 · raw file

  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 GART pte entry
  28. * @pfn: page number to encode
  29. * @type: type of memory in the GART
  30. *
  31. * Set the GART 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 GART entries for a gtt_range
  46. * @dev: our DRM device
  47. * @r: our GTT range
  48. *
  49. * Given a gtt_range object return the GART 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 GART
  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 GART.
  66. *
  67. * FIXME: gtt lock ?
  68. */
  69. static int psb_gtt_insert(struct drm_device *dev, struct gtt_range *r)
  70. {
  71. struct drm_psb_private *dev_priv = dev->dev_private;
  72. u32 *gtt_slot, pte;
  73. int numpages = (r->resource.end + 1 - r->resource.start) >> PAGE_SHIFT;
  74. struct page **pages;
  75. int i;
  76. if (r->pages == NULL) {
  77. WARN_ON(1);
  78. return -EINVAL;
  79. }
  80. WARN_ON(r->stolen); /* refcount these maybe ? */
  81. gtt_slot = psb_gtt_entry(dev, r);
  82. pages = r->pages;
  83. /* Make sure we have no alias present */
  84. wbinvd();
  85. /* Write our page entries into the GART itself */
  86. for (i = 0; i < numpages; i++) {
  87. pte = psb_gtt_mask_pte(page_to_pfn(*pages++), 0/*type*/);
  88. iowrite32(pte, gtt_slot++);
  89. }
  90. /* Make sure all the entries are set before we return */
  91. ioread32(gtt_slot - 1);
  92. return 0;
  93. }
  94. /**
  95. * psb_gtt_remove - remove an object from the GART
  96. * @dev: our DRM device
  97. * @r: our GTT range
  98. *
  99. * Remove a preallocated GTT range from the GART. Overwrite all the
  100. * page table entries with the dummy page
  101. */
  102. static void psb_gtt_remove(struct drm_device *dev, struct gtt_range *r)
  103. {
  104. struct drm_psb_private *dev_priv = dev->dev_private;
  105. u32 *gtt_slot, pte;
  106. int numpages = (r->resource.end + 1 - r->resource.start) >> PAGE_SHIFT;
  107. int i;
  108. WARN_ON(r->stolen);
  109. gtt_slot = psb_gtt_entry(dev, r);
  110. pte = psb_gtt_mask_pte(page_to_pfn(dev_priv->scratch_page), 0);;
  111. for (i = 0; i < numpages; i++)
  112. iowrite32(pte, gtt_slot++);
  113. ioread32(gtt_slot - 1);
  114. }
  115. /**
  116. * psb_gtt_attach_pages - attach and pin GEM pages
  117. * @gt: the gtt range
  118. *
  119. * Pin and build an in kernel list of the pages that back our GEM object.
  120. * While we hold this the pages cannot be swapped out
  121. *
  122. * FIXME: Do we need to cache flush when we update the GTT
  123. */
  124. static int psb_gtt_attach_pages(struct gtt_range *gt)
  125. {
  126. struct inode *inode;
  127. struct address_space *mapping;
  128. int i;
  129. struct page *p;
  130. int pages = (gt->resource.end + 1 - gt->resource.start) >> PAGE_SHIFT;
  131. WARN_ON(gt->pages);
  132. /* This is the shared memory object that backs the GEM resource */
  133. inode = gt->gem.filp->f_path.dentry->d_inode;
  134. mapping = inode->i_mapping;
  135. gt->pages = kmalloc(pages * sizeof(struct page *), GFP_KERNEL);
  136. if (gt->pages == NULL)
  137. return -ENOMEM;
  138. for (i = 0; i < pages; i++) {
  139. /* FIXME: review flags later */
  140. p = read_cache_page_gfp(mapping, i,
  141. __GFP_COLD | GFP_KERNEL);
  142. if (IS_ERR(p))
  143. goto err;
  144. gt->pages[i] = p;
  145. }
  146. return 0;
  147. err:
  148. while (i--)
  149. page_cache_release(gt->pages[i]);
  150. kfree(gt->pages);
  151. gt->pages = NULL;
  152. return PTR_ERR(p);
  153. }
  154. /**
  155. * psb_gtt_detach_pages - attach and pin GEM pages
  156. * @gt: the gtt range
  157. *
  158. * Undo the effect of psb_gtt_attach_pages. At this point the pages
  159. * must have been removed from the GART as they could now be paged out
  160. * and move bus address.
  161. *
  162. * FIXME: Do we need to cache flush when we update the GTT
  163. */
  164. static void psb_gtt_detach_pages(struct gtt_range *gt)
  165. {
  166. int i;
  167. int pages = (gt->resource.end + 1 - gt->resource.start) >> PAGE_SHIFT;
  168. for (i = 0; i < pages; i++) {
  169. /* FIXME: do we need to force dirty */
  170. set_page_dirty(gt->pages[i]);
  171. /* Undo the reference we took when populating the table */
  172. page_cache_release(gt->pages[i]);
  173. }
  174. kfree(gt->pages);
  175. gt->pages = NULL;
  176. }
  177. /**
  178. * psb_gtt_pin - pin pages into the GTT
  179. * @gt: range to pin
  180. *
  181. * Pin a set of pages into the GTT. The pins are refcounted so that
  182. * multiple pins need multiple unpins to undo.
  183. *
  184. * Non GEM backed objects treat this as a no-op as they are always GTT
  185. * backed objects.
  186. */
  187. int psb_gtt_pin(struct gtt_range *gt)
  188. {
  189. int ret;
  190. struct drm_device *dev = gt->gem.dev;
  191. struct drm_psb_private *dev_priv = dev->dev_private;
  192. mutex_lock(&dev_priv->gtt_mutex);
  193. if (gt->in_gart == 0 && gt->stolen == 0) {
  194. ret = psb_gtt_attach_pages(gt);
  195. if (ret < 0)
  196. goto out;
  197. ret = psb_gtt_insert(dev, gt);
  198. if (ret < 0) {
  199. psb_gtt_detach_pages(gt);
  200. goto out;
  201. }
  202. }
  203. gt->in_gart++;
  204. out:
  205. mutex_unlock(&dev_priv->gtt_mutex);
  206. return ret;
  207. }
  208. /**
  209. * psb_gtt_unpin - Drop a GTT pin requirement
  210. * @gt: range to pin
  211. *
  212. * Undoes the effect of psb_gtt_pin. On the last drop the GEM object
  213. * will be removed from the GTT which will also drop the page references
  214. * and allow the VM to clean up or page stuff.
  215. *
  216. * Non GEM backed objects treat this as a no-op as they are always GTT
  217. * backed objects.
  218. */
  219. void psb_gtt_unpin(struct gtt_range *gt)
  220. {
  221. struct drm_device *dev = gt->gem.dev;
  222. struct drm_psb_private *dev_priv = dev->dev_private;
  223. mutex_lock(&dev_priv->gtt_mutex);
  224. WARN_ON(!gt->in_gart);
  225. gt->in_gart--;
  226. if (gt->in_gart == 0 && gt->stolen == 0) {
  227. psb_gtt_remove(dev, gt);
  228. psb_gtt_detach_pages(gt);
  229. }
  230. mutex_unlock(&dev_priv->gtt_mutex);
  231. }
  232. /*
  233. * GTT resource allocator - allocate and manage GTT address space
  234. */
  235. /**
  236. * psb_gtt_alloc_range - allocate GTT address space
  237. * @dev: Our DRM device
  238. * @len: length (bytes) of address space required
  239. * @name: resource name
  240. * @backed: resource should be backed by stolen pages
  241. *
  242. * Ask the kernel core to find us a suitable range of addresses
  243. * to use for a GTT mapping.
  244. *
  245. * Returns a gtt_range structure describing the object, or NULL on
  246. * error. On successful return the resource is both allocated and marked
  247. * as in use.
  248. */
  249. struct gtt_range *psb_gtt_alloc_range(struct drm_device *dev, int len,
  250. const char *name, int backed)
  251. {
  252. struct drm_psb_private *dev_priv = dev->dev_private;
  253. struct gtt_range *gt;
  254. struct resource *r = dev_priv->gtt_mem;
  255. int ret;
  256. unsigned long start, end;
  257. if (backed) {
  258. /* The start of the GTT is the stolen pages */
  259. start = r->start;
  260. end = r->start + dev_priv->pg->stolen_size - 1;
  261. } else {
  262. /* The rest we will use for GEM backed objects */
  263. start = r->start + dev_priv->pg->stolen_size;
  264. end = r->end;
  265. }
  266. gt = kzalloc(sizeof(struct gtt_range), GFP_KERNEL);
  267. if (gt == NULL)
  268. return NULL;
  269. gt->resource.name = name;
  270. gt->stolen = backed;
  271. gt->in_gart = backed;
  272. /* Ensure this is set for non GEM objects */
  273. gt->gem.dev = dev;
  274. kref_init(&gt->kref);
  275. ret = allocate_resource(dev_priv->gtt_mem, &gt->resource,
  276. len, start, end, PAGE_SIZE, NULL, NULL);
  277. if (ret == 0) {
  278. gt->offset = gt->resource.start - r->start;
  279. return gt;
  280. }
  281. kfree(gt);
  282. return NULL;
  283. }
  284. /**
  285. * psb_gtt_destroy - final free up of a gtt
  286. * @kref: the kref of the gtt
  287. *
  288. * Called from the kernel kref put when the final reference to our
  289. * GTT object is dropped. At that point we can free up the resources.
  290. *
  291. * For now we handle mmap clean up here to work around limits in GEM
  292. */
  293. static void psb_gtt_destroy(struct kref *kref)
  294. {
  295. struct gtt_range *gt = container_of(kref, struct gtt_range, kref);
  296. /* Undo the mmap pin if we are destroying the object */
  297. if (gt->mmapping) {
  298. psb_gtt_unpin(gt);
  299. gt->mmapping = 0;
  300. }
  301. WARN_ON(gt->in_gart && !gt->stolen);
  302. release_resource(&gt->resource);
  303. kfree(gt);
  304. }
  305. /**
  306. * psb_gtt_kref_put - drop reference to a GTT object
  307. * @gt: the GT being dropped
  308. *
  309. * Drop a reference to a psb gtt
  310. */
  311. void psb_gtt_kref_put(struct gtt_range *gt)
  312. {
  313. kref_put(&gt->kref, psb_gtt_destroy);
  314. }
  315. /**
  316. * psb_gtt_free_range - release GTT address space
  317. * @dev: our DRM device
  318. * @gt: a mapping created with psb_gtt_alloc_range
  319. *
  320. * Release a resource that was allocated with psb_gtt_alloc_range
  321. */
  322. void psb_gtt_free_range(struct drm_device *dev, struct gtt_range *gt)
  323. {
  324. psb_gtt_kref_put(gt);
  325. }
  326. struct psb_gtt *psb_gtt_alloc(struct drm_device *dev)
  327. {
  328. struct psb_gtt *tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  329. if (!tmp)
  330. return NULL;
  331. init_rwsem(&tmp->sem);
  332. tmp->dev = dev;
  333. return tmp;
  334. }
  335. void psb_gtt_takedown(struct drm_device *dev)
  336. {
  337. struct drm_psb_private *dev_priv = dev->dev_private;
  338. /* FIXME: iounmap dev_priv->vram_addr etc */
  339. if (dev_priv->gtt_map) {
  340. iounmap(dev_priv->gtt_map);
  341. dev_priv->gtt_map = NULL;
  342. }
  343. if (dev_priv->gtt_initialized) {
  344. pci_write_config_word(dev->pdev, PSB_GMCH_CTRL,
  345. dev_priv->gmch_ctrl);
  346. PSB_WVDC32(dev_priv->pge_ctl, PSB_PGETBL_CTL);
  347. (void) PSB_RVDC32(PSB_PGETBL_CTL);
  348. }
  349. kfree(dev_priv->pg);
  350. dev_priv->pg = NULL;
  351. }
  352. int psb_gtt_init(struct drm_device *dev, int resume)
  353. {
  354. struct drm_psb_private *dev_priv = dev->dev_private;
  355. unsigned gtt_pages;
  356. unsigned long stolen_size, vram_stolen_size;
  357. unsigned i, num_pages;
  358. unsigned pfn_base;
  359. uint32_t vram_pages;
  360. uint32_t tt_pages;
  361. uint32_t *ttm_gtt_map;
  362. uint32_t dvmt_mode = 0;
  363. struct psb_gtt *pg;
  364. int ret = 0;
  365. uint32_t pte;
  366. mutex_init(&dev_priv->gtt_mutex);
  367. dev_priv->pg = pg = psb_gtt_alloc(dev);
  368. if (pg == NULL)
  369. return -ENOMEM;
  370. pci_read_config_word(dev->pdev, PSB_GMCH_CTRL, &dev_priv->gmch_ctrl);
  371. pci_write_config_word(dev->pdev, PSB_GMCH_CTRL,
  372. dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
  373. dev_priv->pge_ctl = PSB_RVDC32(PSB_PGETBL_CTL);
  374. PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
  375. (void) PSB_RVDC32(PSB_PGETBL_CTL);
  376. /* The root resource we allocate address space from */
  377. dev_priv->gtt_mem = &dev->pdev->resource[PSB_GATT_RESOURCE];
  378. dev_priv->gtt_initialized = 1;
  379. pg->gtt_phys_start = dev_priv->pge_ctl & PAGE_MASK;
  380. pg->gatt_start = pci_resource_start(dev->pdev, PSB_GATT_RESOURCE);
  381. /* fix me: video mmu has hw bug to access 0x0D0000000,
  382. * then make gatt start at 0x0e000,0000 */
  383. pg->mmu_gatt_start = 0xE0000000;
  384. pg->gtt_start = pci_resource_start(dev->pdev, PSB_GTT_RESOURCE);
  385. gtt_pages =
  386. pci_resource_len(dev->pdev, PSB_GTT_RESOURCE) >> PAGE_SHIFT;
  387. pg->gatt_pages = pci_resource_len(dev->pdev, PSB_GATT_RESOURCE)
  388. >> PAGE_SHIFT;
  389. pci_read_config_dword(dev->pdev, PSB_BSM, &dev_priv->stolen_base);
  390. vram_stolen_size = pg->gtt_phys_start - dev_priv->stolen_base - PAGE_SIZE;
  391. stolen_size = vram_stolen_size;
  392. printk(KERN_INFO"GMMADR(region 0) start: 0x%08x (%dM).\n",
  393. pg->gatt_start, pg->gatt_pages/256);
  394. printk(KERN_INFO"GTTADR(region 3) start: 0x%08x (can map %dM RAM), and actual RAM base 0x%08x.\n",
  395. pg->gtt_start, gtt_pages * 4, pg->gtt_phys_start);
  396. printk(KERN_INFO "Stolen memory information\n");
  397. printk(KERN_INFO " base in RAM: 0x%x\n", dev_priv->stolen_base);
  398. printk(KERN_INFO " size: %luK, calculated by (GTT RAM base) - (Stolen base), seems wrong\n",
  399. vram_stolen_size/1024);
  400. dvmt_mode = (dev_priv->gmch_ctrl >> 4) & 0x7;
  401. printk(KERN_INFO " the correct size should be: %dM(dvmt mode=%d)\n",
  402. (dvmt_mode == 1) ? 1 : (2 << (dvmt_mode - 1)), dvmt_mode);
  403. if (resume && (gtt_pages != pg->gtt_pages) &&
  404. (stolen_size != pg->stolen_size)) {
  405. DRM_ERROR("GTT resume error.\n");
  406. ret = -EINVAL;
  407. goto out_err;
  408. }
  409. pg->gtt_pages = gtt_pages;
  410. pg->stolen_size = stolen_size;
  411. dev_priv->vram_stolen_size = vram_stolen_size;
  412. dev_priv->gtt_map =
  413. ioremap_nocache(pg->gtt_phys_start, gtt_pages << PAGE_SHIFT);
  414. if (!dev_priv->gtt_map) {
  415. DRM_ERROR("Failure to map gtt.\n");
  416. ret = -ENOMEM;
  417. goto out_err;
  418. }
  419. dev_priv->vram_addr = ioremap_wc(dev_priv->stolen_base, stolen_size);
  420. if (!dev_priv->vram_addr) {
  421. DRM_ERROR("Failure to map stolen base.\n");
  422. ret = -ENOMEM;
  423. goto out_err;
  424. }
  425. DRM_DEBUG("%s: vram kernel virtual address %p\n", dev_priv->vram_addr);
  426. tt_pages = (pg->gatt_pages < PSB_TT_PRIV0_PLIMIT) ?
  427. (pg->gatt_pages) : PSB_TT_PRIV0_PLIMIT;
  428. ttm_gtt_map = dev_priv->gtt_map + tt_pages / 2;
  429. /*
  430. * insert vram stolen pages.
  431. */
  432. pfn_base = dev_priv->stolen_base >> PAGE_SHIFT;
  433. vram_pages = num_pages = vram_stolen_size >> PAGE_SHIFT;
  434. printk(KERN_INFO"Set up %d stolen pages starting at 0x%08x, GTT offset %dK\n",
  435. num_pages, pfn_base, 0);
  436. for (i = 0; i < num_pages; ++i) {
  437. pte = psb_gtt_mask_pte(pfn_base + i, 0);
  438. iowrite32(pte, dev_priv->gtt_map + i);
  439. }
  440. /*
  441. * Init rest of gtt managed by IMG.
  442. */
  443. pfn_base = page_to_pfn(dev_priv->scratch_page);
  444. pte = psb_gtt_mask_pte(pfn_base, 0);
  445. for (; i < tt_pages / 2 - 1; ++i)
  446. iowrite32(pte, dev_priv->gtt_map + i);
  447. /*
  448. * Init rest of gtt managed by TTM.
  449. */
  450. pfn_base = page_to_pfn(dev_priv->scratch_page);
  451. pte = psb_gtt_mask_pte(pfn_base, 0);
  452. PSB_DEBUG_INIT("Initializing the rest of a total "
  453. "of %d gtt pages.\n", pg->gatt_pages);
  454. for (; i < pg->gatt_pages - tt_pages / 2; ++i)
  455. iowrite32(pte, ttm_gtt_map + i);
  456. (void) ioread32(dev_priv->gtt_map + i - 1);
  457. return 0;
  458. out_err:
  459. psb_gtt_takedown(dev);
  460. return ret;
  461. }