huge_pages.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. /*
  2. * Copyright © 2017 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include "../i915_selftest.h"
  25. #include <linux/prime_numbers.h>
  26. #include "mock_drm.h"
  27. #include "i915_random.h"
  28. static const unsigned int page_sizes[] = {
  29. I915_GTT_PAGE_SIZE_2M,
  30. I915_GTT_PAGE_SIZE_64K,
  31. I915_GTT_PAGE_SIZE_4K,
  32. };
  33. static unsigned int get_largest_page_size(struct drm_i915_private *i915,
  34. u64 rem)
  35. {
  36. int i;
  37. for (i = 0; i < ARRAY_SIZE(page_sizes); ++i) {
  38. unsigned int page_size = page_sizes[i];
  39. if (HAS_PAGE_SIZES(i915, page_size) && rem >= page_size)
  40. return page_size;
  41. }
  42. return 0;
  43. }
  44. static void huge_pages_free_pages(struct sg_table *st)
  45. {
  46. struct scatterlist *sg;
  47. for (sg = st->sgl; sg; sg = __sg_next(sg)) {
  48. if (sg_page(sg))
  49. __free_pages(sg_page(sg), get_order(sg->length));
  50. }
  51. sg_free_table(st);
  52. kfree(st);
  53. }
  54. static int get_huge_pages(struct drm_i915_gem_object *obj)
  55. {
  56. #define GFP (GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY)
  57. unsigned int page_mask = obj->mm.page_mask;
  58. struct sg_table *st;
  59. struct scatterlist *sg;
  60. unsigned int sg_page_sizes;
  61. u64 rem;
  62. st = kmalloc(sizeof(*st), GFP);
  63. if (!st)
  64. return -ENOMEM;
  65. if (sg_alloc_table(st, obj->base.size >> PAGE_SHIFT, GFP)) {
  66. kfree(st);
  67. return -ENOMEM;
  68. }
  69. rem = obj->base.size;
  70. sg = st->sgl;
  71. st->nents = 0;
  72. sg_page_sizes = 0;
  73. /*
  74. * Our goal here is simple, we want to greedily fill the object from
  75. * largest to smallest page-size, while ensuring that we use *every*
  76. * page-size as per the given page-mask.
  77. */
  78. do {
  79. unsigned int bit = ilog2(page_mask);
  80. unsigned int page_size = BIT(bit);
  81. int order = get_order(page_size);
  82. do {
  83. struct page *page;
  84. GEM_BUG_ON(order >= MAX_ORDER);
  85. page = alloc_pages(GFP | __GFP_ZERO, order);
  86. if (!page)
  87. goto err;
  88. sg_set_page(sg, page, page_size, 0);
  89. sg_page_sizes |= page_size;
  90. st->nents++;
  91. rem -= page_size;
  92. if (!rem) {
  93. sg_mark_end(sg);
  94. break;
  95. }
  96. sg = __sg_next(sg);
  97. } while ((rem - ((page_size-1) & page_mask)) >= page_size);
  98. page_mask &= (page_size-1);
  99. } while (page_mask);
  100. if (i915_gem_gtt_prepare_pages(obj, st))
  101. goto err;
  102. obj->mm.madv = I915_MADV_DONTNEED;
  103. GEM_BUG_ON(sg_page_sizes != obj->mm.page_mask);
  104. __i915_gem_object_set_pages(obj, st, sg_page_sizes);
  105. return 0;
  106. err:
  107. sg_set_page(sg, NULL, 0, 0);
  108. sg_mark_end(sg);
  109. huge_pages_free_pages(st);
  110. return -ENOMEM;
  111. }
  112. static void put_huge_pages(struct drm_i915_gem_object *obj,
  113. struct sg_table *pages)
  114. {
  115. i915_gem_gtt_finish_pages(obj, pages);
  116. huge_pages_free_pages(pages);
  117. obj->mm.dirty = false;
  118. obj->mm.madv = I915_MADV_WILLNEED;
  119. }
  120. static const struct drm_i915_gem_object_ops huge_page_ops = {
  121. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
  122. I915_GEM_OBJECT_IS_SHRINKABLE,
  123. .get_pages = get_huge_pages,
  124. .put_pages = put_huge_pages,
  125. };
  126. static struct drm_i915_gem_object *
  127. huge_pages_object(struct drm_i915_private *i915,
  128. u64 size,
  129. unsigned int page_mask)
  130. {
  131. struct drm_i915_gem_object *obj;
  132. GEM_BUG_ON(!size);
  133. GEM_BUG_ON(!IS_ALIGNED(size, BIT(__ffs(page_mask))));
  134. if (size >> PAGE_SHIFT > INT_MAX)
  135. return ERR_PTR(-E2BIG);
  136. if (overflows_type(size, obj->base.size))
  137. return ERR_PTR(-E2BIG);
  138. obj = i915_gem_object_alloc(i915);
  139. if (!obj)
  140. return ERR_PTR(-ENOMEM);
  141. drm_gem_private_object_init(&i915->drm, &obj->base, size);
  142. i915_gem_object_init(obj, &huge_page_ops);
  143. obj->write_domain = I915_GEM_DOMAIN_CPU;
  144. obj->read_domains = I915_GEM_DOMAIN_CPU;
  145. obj->cache_level = I915_CACHE_NONE;
  146. obj->mm.page_mask = page_mask;
  147. return obj;
  148. }
  149. static int fake_get_huge_pages(struct drm_i915_gem_object *obj)
  150. {
  151. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  152. const u64 max_len = rounddown_pow_of_two(UINT_MAX);
  153. struct sg_table *st;
  154. struct scatterlist *sg;
  155. unsigned int sg_page_sizes;
  156. u64 rem;
  157. st = kmalloc(sizeof(*st), GFP);
  158. if (!st)
  159. return -ENOMEM;
  160. if (sg_alloc_table(st, obj->base.size >> PAGE_SHIFT, GFP)) {
  161. kfree(st);
  162. return -ENOMEM;
  163. }
  164. /* Use optimal page sized chunks to fill in the sg table */
  165. rem = obj->base.size;
  166. sg = st->sgl;
  167. st->nents = 0;
  168. sg_page_sizes = 0;
  169. do {
  170. unsigned int page_size = get_largest_page_size(i915, rem);
  171. unsigned int len = min(page_size * div_u64(rem, page_size),
  172. max_len);
  173. GEM_BUG_ON(!page_size);
  174. sg->offset = 0;
  175. sg->length = len;
  176. sg_dma_len(sg) = len;
  177. sg_dma_address(sg) = page_size;
  178. sg_page_sizes |= len;
  179. st->nents++;
  180. rem -= len;
  181. if (!rem) {
  182. sg_mark_end(sg);
  183. break;
  184. }
  185. sg = sg_next(sg);
  186. } while (1);
  187. i915_sg_trim(st);
  188. obj->mm.madv = I915_MADV_DONTNEED;
  189. __i915_gem_object_set_pages(obj, st, sg_page_sizes);
  190. return 0;
  191. }
  192. static int fake_get_huge_pages_single(struct drm_i915_gem_object *obj)
  193. {
  194. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  195. struct sg_table *st;
  196. struct scatterlist *sg;
  197. unsigned int page_size;
  198. st = kmalloc(sizeof(*st), GFP);
  199. if (!st)
  200. return -ENOMEM;
  201. if (sg_alloc_table(st, 1, GFP)) {
  202. kfree(st);
  203. return -ENOMEM;
  204. }
  205. sg = st->sgl;
  206. st->nents = 1;
  207. page_size = get_largest_page_size(i915, obj->base.size);
  208. GEM_BUG_ON(!page_size);
  209. sg->offset = 0;
  210. sg->length = obj->base.size;
  211. sg_dma_len(sg) = obj->base.size;
  212. sg_dma_address(sg) = page_size;
  213. obj->mm.madv = I915_MADV_DONTNEED;
  214. __i915_gem_object_set_pages(obj, st, sg->length);
  215. return 0;
  216. #undef GFP
  217. }
  218. static void fake_free_huge_pages(struct drm_i915_gem_object *obj,
  219. struct sg_table *pages)
  220. {
  221. sg_free_table(pages);
  222. kfree(pages);
  223. }
  224. static void fake_put_huge_pages(struct drm_i915_gem_object *obj,
  225. struct sg_table *pages)
  226. {
  227. fake_free_huge_pages(obj, pages);
  228. obj->mm.dirty = false;
  229. obj->mm.madv = I915_MADV_WILLNEED;
  230. }
  231. static const struct drm_i915_gem_object_ops fake_ops = {
  232. .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
  233. .get_pages = fake_get_huge_pages,
  234. .put_pages = fake_put_huge_pages,
  235. };
  236. static const struct drm_i915_gem_object_ops fake_ops_single = {
  237. .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
  238. .get_pages = fake_get_huge_pages_single,
  239. .put_pages = fake_put_huge_pages,
  240. };
  241. static struct drm_i915_gem_object *
  242. fake_huge_pages_object(struct drm_i915_private *i915, u64 size, bool single)
  243. {
  244. struct drm_i915_gem_object *obj;
  245. GEM_BUG_ON(!size);
  246. GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
  247. if (size >> PAGE_SHIFT > UINT_MAX)
  248. return ERR_PTR(-E2BIG);
  249. if (overflows_type(size, obj->base.size))
  250. return ERR_PTR(-E2BIG);
  251. obj = i915_gem_object_alloc(i915);
  252. if (!obj)
  253. return ERR_PTR(-ENOMEM);
  254. drm_gem_private_object_init(&i915->drm, &obj->base, size);
  255. if (single)
  256. i915_gem_object_init(obj, &fake_ops_single);
  257. else
  258. i915_gem_object_init(obj, &fake_ops);
  259. obj->write_domain = I915_GEM_DOMAIN_CPU;
  260. obj->read_domains = I915_GEM_DOMAIN_CPU;
  261. obj->cache_level = I915_CACHE_NONE;
  262. return obj;
  263. }
  264. static int igt_check_page_sizes(struct i915_vma *vma)
  265. {
  266. struct drm_i915_private *i915 = vma->vm->i915;
  267. unsigned int supported = INTEL_INFO(i915)->page_sizes;
  268. struct drm_i915_gem_object *obj = vma->obj;
  269. int err = 0;
  270. if (!HAS_PAGE_SIZES(i915, vma->page_sizes.sg)) {
  271. pr_err("unsupported page_sizes.sg=%u, supported=%u\n",
  272. vma->page_sizes.sg & ~supported, supported);
  273. err = -EINVAL;
  274. }
  275. if (!HAS_PAGE_SIZES(i915, vma->page_sizes.gtt)) {
  276. pr_err("unsupported page_sizes.gtt=%u, supported=%u\n",
  277. vma->page_sizes.gtt & ~supported, supported);
  278. err = -EINVAL;
  279. }
  280. if (vma->page_sizes.phys != obj->mm.page_sizes.phys) {
  281. pr_err("vma->page_sizes.phys(%u) != obj->mm.page_sizes.phys(%u)\n",
  282. vma->page_sizes.phys, obj->mm.page_sizes.phys);
  283. err = -EINVAL;
  284. }
  285. if (vma->page_sizes.sg != obj->mm.page_sizes.sg) {
  286. pr_err("vma->page_sizes.sg(%u) != obj->mm.page_sizes.sg(%u)\n",
  287. vma->page_sizes.sg, obj->mm.page_sizes.sg);
  288. err = -EINVAL;
  289. }
  290. if (obj->mm.page_sizes.gtt) {
  291. pr_err("obj->page_sizes.gtt(%u) should never be set\n",
  292. obj->mm.page_sizes.gtt);
  293. err = -EINVAL;
  294. }
  295. return err;
  296. }
  297. static int igt_mock_exhaust_device_supported_pages(void *arg)
  298. {
  299. struct i915_hw_ppgtt *ppgtt = arg;
  300. struct drm_i915_private *i915 = ppgtt->vm.i915;
  301. unsigned int saved_mask = INTEL_INFO(i915)->page_sizes;
  302. struct drm_i915_gem_object *obj;
  303. struct i915_vma *vma;
  304. int i, j, single;
  305. int err;
  306. /*
  307. * Sanity check creating objects with every valid page support
  308. * combination for our mock device.
  309. */
  310. for (i = 1; i < BIT(ARRAY_SIZE(page_sizes)); i++) {
  311. unsigned int combination = 0;
  312. for (j = 0; j < ARRAY_SIZE(page_sizes); j++) {
  313. if (i & BIT(j))
  314. combination |= page_sizes[j];
  315. }
  316. mkwrite_device_info(i915)->page_sizes = combination;
  317. for (single = 0; single <= 1; ++single) {
  318. obj = fake_huge_pages_object(i915, combination, !!single);
  319. if (IS_ERR(obj)) {
  320. err = PTR_ERR(obj);
  321. goto out_device;
  322. }
  323. if (obj->base.size != combination) {
  324. pr_err("obj->base.size=%zu, expected=%u\n",
  325. obj->base.size, combination);
  326. err = -EINVAL;
  327. goto out_put;
  328. }
  329. vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
  330. if (IS_ERR(vma)) {
  331. err = PTR_ERR(vma);
  332. goto out_put;
  333. }
  334. err = i915_vma_pin(vma, 0, 0, PIN_USER);
  335. if (err)
  336. goto out_close;
  337. err = igt_check_page_sizes(vma);
  338. if (vma->page_sizes.sg != combination) {
  339. pr_err("page_sizes.sg=%u, expected=%u\n",
  340. vma->page_sizes.sg, combination);
  341. err = -EINVAL;
  342. }
  343. i915_vma_unpin(vma);
  344. i915_vma_close(vma);
  345. i915_gem_object_put(obj);
  346. if (err)
  347. goto out_device;
  348. }
  349. }
  350. goto out_device;
  351. out_close:
  352. i915_vma_close(vma);
  353. out_put:
  354. i915_gem_object_put(obj);
  355. out_device:
  356. mkwrite_device_info(i915)->page_sizes = saved_mask;
  357. return err;
  358. }
  359. static int igt_mock_ppgtt_misaligned_dma(void *arg)
  360. {
  361. struct i915_hw_ppgtt *ppgtt = arg;
  362. struct drm_i915_private *i915 = ppgtt->vm.i915;
  363. unsigned long supported = INTEL_INFO(i915)->page_sizes;
  364. struct drm_i915_gem_object *obj;
  365. int bit;
  366. int err;
  367. /*
  368. * Sanity check dma misalignment for huge pages -- the dma addresses we
  369. * insert into the paging structures need to always respect the page
  370. * size alignment.
  371. */
  372. bit = ilog2(I915_GTT_PAGE_SIZE_64K);
  373. for_each_set_bit_from(bit, &supported,
  374. ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
  375. IGT_TIMEOUT(end_time);
  376. unsigned int page_size = BIT(bit);
  377. unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
  378. unsigned int offset;
  379. unsigned int size =
  380. round_up(page_size, I915_GTT_PAGE_SIZE_2M) << 1;
  381. struct i915_vma *vma;
  382. obj = fake_huge_pages_object(i915, size, true);
  383. if (IS_ERR(obj))
  384. return PTR_ERR(obj);
  385. if (obj->base.size != size) {
  386. pr_err("obj->base.size=%zu, expected=%u\n",
  387. obj->base.size, size);
  388. err = -EINVAL;
  389. goto out_put;
  390. }
  391. err = i915_gem_object_pin_pages(obj);
  392. if (err)
  393. goto out_put;
  394. /* Force the page size for this object */
  395. obj->mm.page_sizes.sg = page_size;
  396. vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
  397. if (IS_ERR(vma)) {
  398. err = PTR_ERR(vma);
  399. goto out_unpin;
  400. }
  401. err = i915_vma_pin(vma, 0, 0, flags);
  402. if (err) {
  403. i915_vma_close(vma);
  404. goto out_unpin;
  405. }
  406. err = igt_check_page_sizes(vma);
  407. if (vma->page_sizes.gtt != page_size) {
  408. pr_err("page_sizes.gtt=%u, expected %u\n",
  409. vma->page_sizes.gtt, page_size);
  410. err = -EINVAL;
  411. }
  412. i915_vma_unpin(vma);
  413. if (err) {
  414. i915_vma_close(vma);
  415. goto out_unpin;
  416. }
  417. /*
  418. * Try all the other valid offsets until the next
  419. * boundary -- should always fall back to using 4K
  420. * pages.
  421. */
  422. for (offset = 4096; offset < page_size; offset += 4096) {
  423. err = i915_vma_unbind(vma);
  424. if (err) {
  425. i915_vma_close(vma);
  426. goto out_unpin;
  427. }
  428. err = i915_vma_pin(vma, 0, 0, flags | offset);
  429. if (err) {
  430. i915_vma_close(vma);
  431. goto out_unpin;
  432. }
  433. err = igt_check_page_sizes(vma);
  434. if (vma->page_sizes.gtt != I915_GTT_PAGE_SIZE_4K) {
  435. pr_err("page_sizes.gtt=%u, expected %llu\n",
  436. vma->page_sizes.gtt, I915_GTT_PAGE_SIZE_4K);
  437. err = -EINVAL;
  438. }
  439. i915_vma_unpin(vma);
  440. if (err) {
  441. i915_vma_close(vma);
  442. goto out_unpin;
  443. }
  444. if (igt_timeout(end_time,
  445. "%s timed out at offset %x with page-size %x\n",
  446. __func__, offset, page_size))
  447. break;
  448. }
  449. i915_vma_close(vma);
  450. i915_gem_object_unpin_pages(obj);
  451. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  452. i915_gem_object_put(obj);
  453. }
  454. return 0;
  455. out_unpin:
  456. i915_gem_object_unpin_pages(obj);
  457. out_put:
  458. i915_gem_object_put(obj);
  459. return err;
  460. }
  461. static void close_object_list(struct list_head *objects,
  462. struct i915_hw_ppgtt *ppgtt)
  463. {
  464. struct drm_i915_gem_object *obj, *on;
  465. list_for_each_entry_safe(obj, on, objects, st_link) {
  466. struct i915_vma *vma;
  467. vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
  468. if (!IS_ERR(vma))
  469. i915_vma_close(vma);
  470. list_del(&obj->st_link);
  471. i915_gem_object_unpin_pages(obj);
  472. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  473. i915_gem_object_put(obj);
  474. }
  475. }
  476. static int igt_mock_ppgtt_huge_fill(void *arg)
  477. {
  478. struct i915_hw_ppgtt *ppgtt = arg;
  479. struct drm_i915_private *i915 = ppgtt->vm.i915;
  480. unsigned long max_pages = ppgtt->vm.total >> PAGE_SHIFT;
  481. unsigned long page_num;
  482. bool single = false;
  483. LIST_HEAD(objects);
  484. IGT_TIMEOUT(end_time);
  485. int err = -ENODEV;
  486. for_each_prime_number_from(page_num, 1, max_pages) {
  487. struct drm_i915_gem_object *obj;
  488. u64 size = page_num << PAGE_SHIFT;
  489. struct i915_vma *vma;
  490. unsigned int expected_gtt = 0;
  491. int i;
  492. obj = fake_huge_pages_object(i915, size, single);
  493. if (IS_ERR(obj)) {
  494. err = PTR_ERR(obj);
  495. break;
  496. }
  497. if (obj->base.size != size) {
  498. pr_err("obj->base.size=%zd, expected=%llu\n",
  499. obj->base.size, size);
  500. i915_gem_object_put(obj);
  501. err = -EINVAL;
  502. break;
  503. }
  504. err = i915_gem_object_pin_pages(obj);
  505. if (err) {
  506. i915_gem_object_put(obj);
  507. break;
  508. }
  509. list_add(&obj->st_link, &objects);
  510. vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
  511. if (IS_ERR(vma)) {
  512. err = PTR_ERR(vma);
  513. break;
  514. }
  515. err = i915_vma_pin(vma, 0, 0, PIN_USER);
  516. if (err)
  517. break;
  518. err = igt_check_page_sizes(vma);
  519. if (err) {
  520. i915_vma_unpin(vma);
  521. break;
  522. }
  523. /*
  524. * Figure out the expected gtt page size knowing that we go from
  525. * largest to smallest page size sg chunks, and that we align to
  526. * the largest page size.
  527. */
  528. for (i = 0; i < ARRAY_SIZE(page_sizes); ++i) {
  529. unsigned int page_size = page_sizes[i];
  530. if (HAS_PAGE_SIZES(i915, page_size) &&
  531. size >= page_size) {
  532. expected_gtt |= page_size;
  533. size &= page_size-1;
  534. }
  535. }
  536. GEM_BUG_ON(!expected_gtt);
  537. GEM_BUG_ON(size);
  538. if (expected_gtt & I915_GTT_PAGE_SIZE_4K)
  539. expected_gtt &= ~I915_GTT_PAGE_SIZE_64K;
  540. i915_vma_unpin(vma);
  541. if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) {
  542. if (!IS_ALIGNED(vma->node.start,
  543. I915_GTT_PAGE_SIZE_2M)) {
  544. pr_err("node.start(%llx) not aligned to 2M\n",
  545. vma->node.start);
  546. err = -EINVAL;
  547. break;
  548. }
  549. if (!IS_ALIGNED(vma->node.size,
  550. I915_GTT_PAGE_SIZE_2M)) {
  551. pr_err("node.size(%llx) not aligned to 2M\n",
  552. vma->node.size);
  553. err = -EINVAL;
  554. break;
  555. }
  556. }
  557. if (vma->page_sizes.gtt != expected_gtt) {
  558. pr_err("gtt=%u, expected=%u, size=%zd, single=%s\n",
  559. vma->page_sizes.gtt, expected_gtt,
  560. obj->base.size, yesno(!!single));
  561. err = -EINVAL;
  562. break;
  563. }
  564. if (igt_timeout(end_time,
  565. "%s timed out at size %zd\n",
  566. __func__, obj->base.size))
  567. break;
  568. single = !single;
  569. }
  570. close_object_list(&objects, ppgtt);
  571. if (err == -ENOMEM || err == -ENOSPC)
  572. err = 0;
  573. return err;
  574. }
  575. static int igt_mock_ppgtt_64K(void *arg)
  576. {
  577. struct i915_hw_ppgtt *ppgtt = arg;
  578. struct drm_i915_private *i915 = ppgtt->vm.i915;
  579. struct drm_i915_gem_object *obj;
  580. const struct object_info {
  581. unsigned int size;
  582. unsigned int gtt;
  583. unsigned int offset;
  584. } objects[] = {
  585. /* Cases with forced padding/alignment */
  586. {
  587. .size = SZ_64K,
  588. .gtt = I915_GTT_PAGE_SIZE_64K,
  589. .offset = 0,
  590. },
  591. {
  592. .size = SZ_64K + SZ_4K,
  593. .gtt = I915_GTT_PAGE_SIZE_4K,
  594. .offset = 0,
  595. },
  596. {
  597. .size = SZ_64K - SZ_4K,
  598. .gtt = I915_GTT_PAGE_SIZE_4K,
  599. .offset = 0,
  600. },
  601. {
  602. .size = SZ_2M,
  603. .gtt = I915_GTT_PAGE_SIZE_64K,
  604. .offset = 0,
  605. },
  606. {
  607. .size = SZ_2M - SZ_4K,
  608. .gtt = I915_GTT_PAGE_SIZE_4K,
  609. .offset = 0,
  610. },
  611. {
  612. .size = SZ_2M + SZ_4K,
  613. .gtt = I915_GTT_PAGE_SIZE_64K | I915_GTT_PAGE_SIZE_4K,
  614. .offset = 0,
  615. },
  616. {
  617. .size = SZ_2M + SZ_64K,
  618. .gtt = I915_GTT_PAGE_SIZE_64K,
  619. .offset = 0,
  620. },
  621. {
  622. .size = SZ_2M - SZ_64K,
  623. .gtt = I915_GTT_PAGE_SIZE_64K,
  624. .offset = 0,
  625. },
  626. /* Try without any forced padding/alignment */
  627. {
  628. .size = SZ_64K,
  629. .offset = SZ_2M,
  630. .gtt = I915_GTT_PAGE_SIZE_4K,
  631. },
  632. {
  633. .size = SZ_128K,
  634. .offset = SZ_2M - SZ_64K,
  635. .gtt = I915_GTT_PAGE_SIZE_4K,
  636. },
  637. };
  638. struct i915_vma *vma;
  639. int i, single;
  640. int err;
  641. /*
  642. * Sanity check some of the trickiness with 64K pages -- either we can
  643. * safely mark the whole page-table(2M block) as 64K, or we have to
  644. * always fallback to 4K.
  645. */
  646. if (!HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K))
  647. return 0;
  648. for (i = 0; i < ARRAY_SIZE(objects); ++i) {
  649. unsigned int size = objects[i].size;
  650. unsigned int expected_gtt = objects[i].gtt;
  651. unsigned int offset = objects[i].offset;
  652. unsigned int flags = PIN_USER;
  653. for (single = 0; single <= 1; single++) {
  654. obj = fake_huge_pages_object(i915, size, !!single);
  655. if (IS_ERR(obj))
  656. return PTR_ERR(obj);
  657. err = i915_gem_object_pin_pages(obj);
  658. if (err)
  659. goto out_object_put;
  660. /*
  661. * Disable 2M pages -- We only want to use 64K/4K pages
  662. * for this test.
  663. */
  664. obj->mm.page_sizes.sg &= ~I915_GTT_PAGE_SIZE_2M;
  665. vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
  666. if (IS_ERR(vma)) {
  667. err = PTR_ERR(vma);
  668. goto out_object_unpin;
  669. }
  670. if (offset)
  671. flags |= PIN_OFFSET_FIXED | offset;
  672. err = i915_vma_pin(vma, 0, 0, flags);
  673. if (err)
  674. goto out_vma_close;
  675. err = igt_check_page_sizes(vma);
  676. if (err)
  677. goto out_vma_unpin;
  678. if (!offset && vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) {
  679. if (!IS_ALIGNED(vma->node.start,
  680. I915_GTT_PAGE_SIZE_2M)) {
  681. pr_err("node.start(%llx) not aligned to 2M\n",
  682. vma->node.start);
  683. err = -EINVAL;
  684. goto out_vma_unpin;
  685. }
  686. if (!IS_ALIGNED(vma->node.size,
  687. I915_GTT_PAGE_SIZE_2M)) {
  688. pr_err("node.size(%llx) not aligned to 2M\n",
  689. vma->node.size);
  690. err = -EINVAL;
  691. goto out_vma_unpin;
  692. }
  693. }
  694. if (vma->page_sizes.gtt != expected_gtt) {
  695. pr_err("gtt=%u, expected=%u, i=%d, single=%s\n",
  696. vma->page_sizes.gtt, expected_gtt, i,
  697. yesno(!!single));
  698. err = -EINVAL;
  699. goto out_vma_unpin;
  700. }
  701. i915_vma_unpin(vma);
  702. i915_vma_close(vma);
  703. i915_gem_object_unpin_pages(obj);
  704. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  705. i915_gem_object_put(obj);
  706. }
  707. }
  708. return 0;
  709. out_vma_unpin:
  710. i915_vma_unpin(vma);
  711. out_vma_close:
  712. i915_vma_close(vma);
  713. out_object_unpin:
  714. i915_gem_object_unpin_pages(obj);
  715. out_object_put:
  716. i915_gem_object_put(obj);
  717. return err;
  718. }
  719. static struct i915_vma *
  720. gpu_write_dw(struct i915_vma *vma, u64 offset, u32 val)
  721. {
  722. struct drm_i915_private *i915 = vma->vm->i915;
  723. const int gen = INTEL_GEN(i915);
  724. unsigned int count = vma->size >> PAGE_SHIFT;
  725. struct drm_i915_gem_object *obj;
  726. struct i915_vma *batch;
  727. unsigned int size;
  728. u32 *cmd;
  729. int n;
  730. int err;
  731. size = (1 + 4 * count) * sizeof(u32);
  732. size = round_up(size, PAGE_SIZE);
  733. obj = i915_gem_object_create_internal(i915, size);
  734. if (IS_ERR(obj))
  735. return ERR_CAST(obj);
  736. err = i915_gem_object_set_to_wc_domain(obj, true);
  737. if (err)
  738. goto err;
  739. cmd = i915_gem_object_pin_map(obj, I915_MAP_WC);
  740. if (IS_ERR(cmd)) {
  741. err = PTR_ERR(cmd);
  742. goto err;
  743. }
  744. offset += vma->node.start;
  745. for (n = 0; n < count; n++) {
  746. if (gen >= 8) {
  747. *cmd++ = MI_STORE_DWORD_IMM_GEN4;
  748. *cmd++ = lower_32_bits(offset);
  749. *cmd++ = upper_32_bits(offset);
  750. *cmd++ = val;
  751. } else if (gen >= 4) {
  752. *cmd++ = MI_STORE_DWORD_IMM_GEN4 |
  753. (gen < 6 ? MI_USE_GGTT : 0);
  754. *cmd++ = 0;
  755. *cmd++ = offset;
  756. *cmd++ = val;
  757. } else {
  758. *cmd++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
  759. *cmd++ = offset;
  760. *cmd++ = val;
  761. }
  762. offset += PAGE_SIZE;
  763. }
  764. *cmd = MI_BATCH_BUFFER_END;
  765. i915_gem_chipset_flush(i915);
  766. i915_gem_object_unpin_map(obj);
  767. batch = i915_vma_instance(obj, vma->vm, NULL);
  768. if (IS_ERR(batch)) {
  769. err = PTR_ERR(batch);
  770. goto err;
  771. }
  772. err = i915_vma_pin(batch, 0, 0, PIN_USER);
  773. if (err)
  774. goto err;
  775. return batch;
  776. err:
  777. i915_gem_object_put(obj);
  778. return ERR_PTR(err);
  779. }
  780. static int gpu_write(struct i915_vma *vma,
  781. struct i915_gem_context *ctx,
  782. struct intel_engine_cs *engine,
  783. u32 dword,
  784. u32 value)
  785. {
  786. struct i915_request *rq;
  787. struct i915_vma *batch;
  788. int flags = 0;
  789. int err;
  790. GEM_BUG_ON(!intel_engine_can_store_dword(engine));
  791. err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
  792. if (err)
  793. return err;
  794. rq = i915_request_alloc(engine, ctx);
  795. if (IS_ERR(rq))
  796. return PTR_ERR(rq);
  797. batch = gpu_write_dw(vma, dword * sizeof(u32), value);
  798. if (IS_ERR(batch)) {
  799. err = PTR_ERR(batch);
  800. goto err_request;
  801. }
  802. err = i915_vma_move_to_active(batch, rq, 0);
  803. if (err)
  804. goto err_request;
  805. i915_gem_object_set_active_reference(batch->obj);
  806. i915_vma_unpin(batch);
  807. i915_vma_close(batch);
  808. err = engine->emit_bb_start(rq,
  809. batch->node.start, batch->node.size,
  810. flags);
  811. if (err)
  812. goto err_request;
  813. err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
  814. if (err)
  815. i915_request_skip(rq, err);
  816. err_request:
  817. i915_request_add(rq);
  818. return err;
  819. }
  820. static int cpu_check(struct drm_i915_gem_object *obj, u32 dword, u32 val)
  821. {
  822. unsigned int needs_flush;
  823. unsigned long n;
  824. int err;
  825. err = i915_gem_obj_prepare_shmem_read(obj, &needs_flush);
  826. if (err)
  827. return err;
  828. for (n = 0; n < obj->base.size >> PAGE_SHIFT; ++n) {
  829. u32 *ptr = kmap_atomic(i915_gem_object_get_page(obj, n));
  830. if (needs_flush & CLFLUSH_BEFORE)
  831. drm_clflush_virt_range(ptr, PAGE_SIZE);
  832. if (ptr[dword] != val) {
  833. pr_err("n=%lu ptr[%u]=%u, val=%u\n",
  834. n, dword, ptr[dword], val);
  835. kunmap_atomic(ptr);
  836. err = -EINVAL;
  837. break;
  838. }
  839. kunmap_atomic(ptr);
  840. }
  841. i915_gem_obj_finish_shmem_access(obj);
  842. return err;
  843. }
  844. static int __igt_write_huge(struct i915_gem_context *ctx,
  845. struct intel_engine_cs *engine,
  846. struct drm_i915_gem_object *obj,
  847. u64 size, u64 offset,
  848. u32 dword, u32 val)
  849. {
  850. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  851. struct i915_address_space *vm =
  852. ctx->ppgtt ? &ctx->ppgtt->vm : &i915->ggtt.vm;
  853. unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
  854. struct i915_vma *vma;
  855. int err;
  856. vma = i915_vma_instance(obj, vm, NULL);
  857. if (IS_ERR(vma))
  858. return PTR_ERR(vma);
  859. err = i915_vma_unbind(vma);
  860. if (err)
  861. goto out_vma_close;
  862. err = i915_vma_pin(vma, size, 0, flags | offset);
  863. if (err) {
  864. /*
  865. * The ggtt may have some pages reserved so
  866. * refrain from erroring out.
  867. */
  868. if (err == -ENOSPC && i915_is_ggtt(vm))
  869. err = 0;
  870. goto out_vma_close;
  871. }
  872. err = igt_check_page_sizes(vma);
  873. if (err)
  874. goto out_vma_unpin;
  875. err = gpu_write(vma, ctx, engine, dword, val);
  876. if (err) {
  877. pr_err("gpu-write failed at offset=%llx\n", offset);
  878. goto out_vma_unpin;
  879. }
  880. err = cpu_check(obj, dword, val);
  881. if (err) {
  882. pr_err("cpu-check failed at offset=%llx\n", offset);
  883. goto out_vma_unpin;
  884. }
  885. out_vma_unpin:
  886. i915_vma_unpin(vma);
  887. out_vma_close:
  888. i915_vma_destroy(vma);
  889. return err;
  890. }
  891. static int igt_write_huge(struct i915_gem_context *ctx,
  892. struct drm_i915_gem_object *obj)
  893. {
  894. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  895. struct i915_address_space *vm =
  896. ctx->ppgtt ? &ctx->ppgtt->vm : &i915->ggtt.vm;
  897. static struct intel_engine_cs *engines[I915_NUM_ENGINES];
  898. struct intel_engine_cs *engine;
  899. I915_RND_STATE(prng);
  900. IGT_TIMEOUT(end_time);
  901. unsigned int max_page_size;
  902. unsigned int id;
  903. u64 max;
  904. u64 num;
  905. u64 size;
  906. int *order;
  907. int i, n;
  908. int err = 0;
  909. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
  910. size = obj->base.size;
  911. if (obj->mm.page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
  912. size = round_up(size, I915_GTT_PAGE_SIZE_2M);
  913. max_page_size = rounddown_pow_of_two(obj->mm.page_sizes.sg);
  914. max = div_u64((vm->total - size), max_page_size);
  915. n = 0;
  916. for_each_engine(engine, i915, id) {
  917. if (!intel_engine_can_store_dword(engine)) {
  918. pr_info("store-dword-imm not supported on engine=%u\n", id);
  919. continue;
  920. }
  921. engines[n++] = engine;
  922. }
  923. if (!n)
  924. return 0;
  925. /*
  926. * To keep things interesting when alternating between engines in our
  927. * randomized order, lets also make feeding to the same engine a few
  928. * times in succession a possibility by enlarging the permutation array.
  929. */
  930. order = i915_random_order(n * I915_NUM_ENGINES, &prng);
  931. if (!order)
  932. return -ENOMEM;
  933. /*
  934. * Try various offsets in an ascending/descending fashion until we
  935. * timeout -- we want to avoid issues hidden by effectively always using
  936. * offset = 0.
  937. */
  938. i = 0;
  939. for_each_prime_number_from(num, 0, max) {
  940. u64 offset_low = num * max_page_size;
  941. u64 offset_high = (max - num) * max_page_size;
  942. u32 dword = offset_in_page(num) / 4;
  943. engine = engines[order[i] % n];
  944. i = (i + 1) % (n * I915_NUM_ENGINES);
  945. err = __igt_write_huge(ctx, engine, obj, size, offset_low, dword, num + 1);
  946. if (err)
  947. break;
  948. err = __igt_write_huge(ctx, engine, obj, size, offset_high, dword, num + 1);
  949. if (err)
  950. break;
  951. if (igt_timeout(end_time,
  952. "%s timed out on engine=%u, offset_low=%llx offset_high=%llx, max_page_size=%x\n",
  953. __func__, engine->id, offset_low, offset_high, max_page_size))
  954. break;
  955. }
  956. kfree(order);
  957. return err;
  958. }
  959. static int igt_ppgtt_exhaust_huge(void *arg)
  960. {
  961. struct i915_gem_context *ctx = arg;
  962. struct drm_i915_private *i915 = ctx->i915;
  963. unsigned long supported = INTEL_INFO(i915)->page_sizes;
  964. static unsigned int pages[ARRAY_SIZE(page_sizes)];
  965. struct drm_i915_gem_object *obj;
  966. unsigned int size_mask;
  967. unsigned int page_mask;
  968. int n, i;
  969. int err = -ENODEV;
  970. if (supported == I915_GTT_PAGE_SIZE_4K)
  971. return 0;
  972. /*
  973. * Sanity check creating objects with a varying mix of page sizes --
  974. * ensuring that our writes lands in the right place.
  975. */
  976. n = 0;
  977. for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1)
  978. pages[n++] = BIT(i);
  979. for (size_mask = 2; size_mask < BIT(n); size_mask++) {
  980. unsigned int size = 0;
  981. for (i = 0; i < n; i++) {
  982. if (size_mask & BIT(i))
  983. size |= pages[i];
  984. }
  985. /*
  986. * For our page mask we want to enumerate all the page-size
  987. * combinations which will fit into our chosen object size.
  988. */
  989. for (page_mask = 2; page_mask <= size_mask; page_mask++) {
  990. unsigned int page_sizes = 0;
  991. for (i = 0; i < n; i++) {
  992. if (page_mask & BIT(i))
  993. page_sizes |= pages[i];
  994. }
  995. /*
  996. * Ensure that we can actually fill the given object
  997. * with our chosen page mask.
  998. */
  999. if (!IS_ALIGNED(size, BIT(__ffs(page_sizes))))
  1000. continue;
  1001. obj = huge_pages_object(i915, size, page_sizes);
  1002. if (IS_ERR(obj)) {
  1003. err = PTR_ERR(obj);
  1004. goto out_device;
  1005. }
  1006. err = i915_gem_object_pin_pages(obj);
  1007. if (err) {
  1008. i915_gem_object_put(obj);
  1009. if (err == -ENOMEM) {
  1010. pr_info("unable to get pages, size=%u, pages=%u\n",
  1011. size, page_sizes);
  1012. err = 0;
  1013. break;
  1014. }
  1015. pr_err("pin_pages failed, size=%u, pages=%u\n",
  1016. size_mask, page_mask);
  1017. goto out_device;
  1018. }
  1019. /* Force the page-size for the gtt insertion */
  1020. obj->mm.page_sizes.sg = page_sizes;
  1021. err = igt_write_huge(ctx, obj);
  1022. if (err) {
  1023. pr_err("exhaust write-huge failed with size=%u\n",
  1024. size);
  1025. goto out_unpin;
  1026. }
  1027. i915_gem_object_unpin_pages(obj);
  1028. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  1029. i915_gem_object_put(obj);
  1030. }
  1031. }
  1032. goto out_device;
  1033. out_unpin:
  1034. i915_gem_object_unpin_pages(obj);
  1035. i915_gem_object_put(obj);
  1036. out_device:
  1037. mkwrite_device_info(i915)->page_sizes = supported;
  1038. return err;
  1039. }
  1040. static int igt_ppgtt_internal_huge(void *arg)
  1041. {
  1042. struct i915_gem_context *ctx = arg;
  1043. struct drm_i915_private *i915 = ctx->i915;
  1044. struct drm_i915_gem_object *obj;
  1045. static const unsigned int sizes[] = {
  1046. SZ_64K,
  1047. SZ_128K,
  1048. SZ_256K,
  1049. SZ_512K,
  1050. SZ_1M,
  1051. SZ_2M,
  1052. };
  1053. int i;
  1054. int err;
  1055. /*
  1056. * Sanity check that the HW uses huge pages correctly through internal
  1057. * -- ensure that our writes land in the right place.
  1058. */
  1059. for (i = 0; i < ARRAY_SIZE(sizes); ++i) {
  1060. unsigned int size = sizes[i];
  1061. obj = i915_gem_object_create_internal(i915, size);
  1062. if (IS_ERR(obj))
  1063. return PTR_ERR(obj);
  1064. err = i915_gem_object_pin_pages(obj);
  1065. if (err)
  1066. goto out_put;
  1067. if (obj->mm.page_sizes.phys < I915_GTT_PAGE_SIZE_64K) {
  1068. pr_info("internal unable to allocate huge-page(s) with size=%u\n",
  1069. size);
  1070. goto out_unpin;
  1071. }
  1072. err = igt_write_huge(ctx, obj);
  1073. if (err) {
  1074. pr_err("internal write-huge failed with size=%u\n",
  1075. size);
  1076. goto out_unpin;
  1077. }
  1078. i915_gem_object_unpin_pages(obj);
  1079. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  1080. i915_gem_object_put(obj);
  1081. }
  1082. return 0;
  1083. out_unpin:
  1084. i915_gem_object_unpin_pages(obj);
  1085. out_put:
  1086. i915_gem_object_put(obj);
  1087. return err;
  1088. }
  1089. static inline bool igt_can_allocate_thp(struct drm_i915_private *i915)
  1090. {
  1091. return i915->mm.gemfs && has_transparent_hugepage();
  1092. }
  1093. static int igt_ppgtt_gemfs_huge(void *arg)
  1094. {
  1095. struct i915_gem_context *ctx = arg;
  1096. struct drm_i915_private *i915 = ctx->i915;
  1097. struct drm_i915_gem_object *obj;
  1098. static const unsigned int sizes[] = {
  1099. SZ_2M,
  1100. SZ_4M,
  1101. SZ_8M,
  1102. SZ_16M,
  1103. SZ_32M,
  1104. };
  1105. int i;
  1106. int err;
  1107. /*
  1108. * Sanity check that the HW uses huge pages correctly through gemfs --
  1109. * ensure that our writes land in the right place.
  1110. */
  1111. if (!igt_can_allocate_thp(i915)) {
  1112. pr_info("missing THP support, skipping\n");
  1113. return 0;
  1114. }
  1115. for (i = 0; i < ARRAY_SIZE(sizes); ++i) {
  1116. unsigned int size = sizes[i];
  1117. obj = i915_gem_object_create(i915, size);
  1118. if (IS_ERR(obj))
  1119. return PTR_ERR(obj);
  1120. err = i915_gem_object_pin_pages(obj);
  1121. if (err)
  1122. goto out_put;
  1123. if (obj->mm.page_sizes.phys < I915_GTT_PAGE_SIZE_2M) {
  1124. pr_info("finishing test early, gemfs unable to allocate huge-page(s) with size=%u\n",
  1125. size);
  1126. goto out_unpin;
  1127. }
  1128. err = igt_write_huge(ctx, obj);
  1129. if (err) {
  1130. pr_err("gemfs write-huge failed with size=%u\n",
  1131. size);
  1132. goto out_unpin;
  1133. }
  1134. i915_gem_object_unpin_pages(obj);
  1135. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  1136. i915_gem_object_put(obj);
  1137. }
  1138. return 0;
  1139. out_unpin:
  1140. i915_gem_object_unpin_pages(obj);
  1141. out_put:
  1142. i915_gem_object_put(obj);
  1143. return err;
  1144. }
  1145. static int igt_ppgtt_pin_update(void *arg)
  1146. {
  1147. struct i915_gem_context *ctx = arg;
  1148. struct drm_i915_private *dev_priv = ctx->i915;
  1149. unsigned long supported = INTEL_INFO(dev_priv)->page_sizes;
  1150. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  1151. struct drm_i915_gem_object *obj;
  1152. struct i915_vma *vma;
  1153. unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
  1154. int first, last;
  1155. int err;
  1156. /*
  1157. * Make sure there's no funny business when doing a PIN_UPDATE -- in the
  1158. * past we had a subtle issue with being able to incorrectly do multiple
  1159. * alloc va ranges on the same object when doing a PIN_UPDATE, which
  1160. * resulted in some pretty nasty bugs, though only when using
  1161. * huge-gtt-pages.
  1162. */
  1163. if (!USES_FULL_48BIT_PPGTT(dev_priv)) {
  1164. pr_info("48b PPGTT not supported, skipping\n");
  1165. return 0;
  1166. }
  1167. first = ilog2(I915_GTT_PAGE_SIZE_64K);
  1168. last = ilog2(I915_GTT_PAGE_SIZE_2M);
  1169. for_each_set_bit_from(first, &supported, last + 1) {
  1170. unsigned int page_size = BIT(first);
  1171. obj = i915_gem_object_create_internal(dev_priv, page_size);
  1172. if (IS_ERR(obj))
  1173. return PTR_ERR(obj);
  1174. vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
  1175. if (IS_ERR(vma)) {
  1176. err = PTR_ERR(vma);
  1177. goto out_put;
  1178. }
  1179. err = i915_vma_pin(vma, SZ_2M, 0, flags);
  1180. if (err)
  1181. goto out_close;
  1182. if (vma->page_sizes.sg < page_size) {
  1183. pr_info("Unable to allocate page-size %x, finishing test early\n",
  1184. page_size);
  1185. goto out_unpin;
  1186. }
  1187. err = igt_check_page_sizes(vma);
  1188. if (err)
  1189. goto out_unpin;
  1190. if (vma->page_sizes.gtt != page_size) {
  1191. dma_addr_t addr = i915_gem_object_get_dma_address(obj, 0);
  1192. /*
  1193. * The only valid reason for this to ever fail would be
  1194. * if the dma-mapper screwed us over when we did the
  1195. * dma_map_sg(), since it has the final say over the dma
  1196. * address.
  1197. */
  1198. if (IS_ALIGNED(addr, page_size)) {
  1199. pr_err("page_sizes.gtt=%u, expected=%u\n",
  1200. vma->page_sizes.gtt, page_size);
  1201. err = -EINVAL;
  1202. } else {
  1203. pr_info("dma address misaligned, finishing test early\n");
  1204. }
  1205. goto out_unpin;
  1206. }
  1207. err = i915_vma_bind(vma, I915_CACHE_NONE, PIN_UPDATE);
  1208. if (err)
  1209. goto out_unpin;
  1210. i915_vma_unpin(vma);
  1211. i915_vma_close(vma);
  1212. i915_gem_object_put(obj);
  1213. }
  1214. obj = i915_gem_object_create_internal(dev_priv, PAGE_SIZE);
  1215. if (IS_ERR(obj))
  1216. return PTR_ERR(obj);
  1217. vma = i915_vma_instance(obj, &ppgtt->vm, NULL);
  1218. if (IS_ERR(vma)) {
  1219. err = PTR_ERR(vma);
  1220. goto out_put;
  1221. }
  1222. err = i915_vma_pin(vma, 0, 0, flags);
  1223. if (err)
  1224. goto out_close;
  1225. /*
  1226. * Make sure we don't end up with something like where the pde is still
  1227. * pointing to the 2M page, and the pt we just filled-in is dangling --
  1228. * we can check this by writing to the first page where it would then
  1229. * land in the now stale 2M page.
  1230. */
  1231. err = gpu_write(vma, ctx, dev_priv->engine[RCS], 0, 0xdeadbeaf);
  1232. if (err)
  1233. goto out_unpin;
  1234. err = cpu_check(obj, 0, 0xdeadbeaf);
  1235. out_unpin:
  1236. i915_vma_unpin(vma);
  1237. out_close:
  1238. i915_vma_close(vma);
  1239. out_put:
  1240. i915_gem_object_put(obj);
  1241. return err;
  1242. }
  1243. static int igt_tmpfs_fallback(void *arg)
  1244. {
  1245. struct i915_gem_context *ctx = arg;
  1246. struct drm_i915_private *i915 = ctx->i915;
  1247. struct vfsmount *gemfs = i915->mm.gemfs;
  1248. struct i915_address_space *vm =
  1249. ctx->ppgtt ? &ctx->ppgtt->vm : &i915->ggtt.vm;
  1250. struct drm_i915_gem_object *obj;
  1251. struct i915_vma *vma;
  1252. u32 *vaddr;
  1253. int err = 0;
  1254. /*
  1255. * Make sure that we don't burst into a ball of flames upon falling back
  1256. * to tmpfs, which we rely on if on the off-chance we encouter a failure
  1257. * when setting up gemfs.
  1258. */
  1259. i915->mm.gemfs = NULL;
  1260. obj = i915_gem_object_create(i915, PAGE_SIZE);
  1261. if (IS_ERR(obj)) {
  1262. err = PTR_ERR(obj);
  1263. goto out_restore;
  1264. }
  1265. vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
  1266. if (IS_ERR(vaddr)) {
  1267. err = PTR_ERR(vaddr);
  1268. goto out_put;
  1269. }
  1270. *vaddr = 0xdeadbeaf;
  1271. i915_gem_object_unpin_map(obj);
  1272. vma = i915_vma_instance(obj, vm, NULL);
  1273. if (IS_ERR(vma)) {
  1274. err = PTR_ERR(vma);
  1275. goto out_put;
  1276. }
  1277. err = i915_vma_pin(vma, 0, 0, PIN_USER);
  1278. if (err)
  1279. goto out_close;
  1280. err = igt_check_page_sizes(vma);
  1281. i915_vma_unpin(vma);
  1282. out_close:
  1283. i915_vma_close(vma);
  1284. out_put:
  1285. i915_gem_object_put(obj);
  1286. out_restore:
  1287. i915->mm.gemfs = gemfs;
  1288. return err;
  1289. }
  1290. static int igt_shrink_thp(void *arg)
  1291. {
  1292. struct i915_gem_context *ctx = arg;
  1293. struct drm_i915_private *i915 = ctx->i915;
  1294. struct i915_address_space *vm =
  1295. ctx->ppgtt ? &ctx->ppgtt->vm : &i915->ggtt.vm;
  1296. struct drm_i915_gem_object *obj;
  1297. struct i915_vma *vma;
  1298. unsigned int flags = PIN_USER;
  1299. int err;
  1300. /*
  1301. * Sanity check shrinking huge-paged object -- make sure nothing blows
  1302. * up.
  1303. */
  1304. if (!igt_can_allocate_thp(i915)) {
  1305. pr_info("missing THP support, skipping\n");
  1306. return 0;
  1307. }
  1308. obj = i915_gem_object_create(i915, SZ_2M);
  1309. if (IS_ERR(obj))
  1310. return PTR_ERR(obj);
  1311. vma = i915_vma_instance(obj, vm, NULL);
  1312. if (IS_ERR(vma)) {
  1313. err = PTR_ERR(vma);
  1314. goto out_put;
  1315. }
  1316. err = i915_vma_pin(vma, 0, 0, flags);
  1317. if (err)
  1318. goto out_close;
  1319. if (obj->mm.page_sizes.phys < I915_GTT_PAGE_SIZE_2M) {
  1320. pr_info("failed to allocate THP, finishing test early\n");
  1321. goto out_unpin;
  1322. }
  1323. err = igt_check_page_sizes(vma);
  1324. if (err)
  1325. goto out_unpin;
  1326. err = gpu_write(vma, ctx, i915->engine[RCS], 0, 0xdeadbeaf);
  1327. if (err)
  1328. goto out_unpin;
  1329. i915_vma_unpin(vma);
  1330. /*
  1331. * Now that the pages are *unpinned* shrink-all should invoke
  1332. * shmem to truncate our pages.
  1333. */
  1334. i915_gem_shrink_all(i915);
  1335. if (i915_gem_object_has_pages(obj)) {
  1336. pr_err("shrink-all didn't truncate the pages\n");
  1337. err = -EINVAL;
  1338. goto out_close;
  1339. }
  1340. if (obj->mm.page_sizes.sg || obj->mm.page_sizes.phys) {
  1341. pr_err("residual page-size bits left\n");
  1342. err = -EINVAL;
  1343. goto out_close;
  1344. }
  1345. err = i915_vma_pin(vma, 0, 0, flags);
  1346. if (err)
  1347. goto out_close;
  1348. err = cpu_check(obj, 0, 0xdeadbeaf);
  1349. out_unpin:
  1350. i915_vma_unpin(vma);
  1351. out_close:
  1352. i915_vma_close(vma);
  1353. out_put:
  1354. i915_gem_object_put(obj);
  1355. return err;
  1356. }
  1357. int i915_gem_huge_page_mock_selftests(void)
  1358. {
  1359. static const struct i915_subtest tests[] = {
  1360. SUBTEST(igt_mock_exhaust_device_supported_pages),
  1361. SUBTEST(igt_mock_ppgtt_misaligned_dma),
  1362. SUBTEST(igt_mock_ppgtt_huge_fill),
  1363. SUBTEST(igt_mock_ppgtt_64K),
  1364. };
  1365. int saved_ppgtt = i915_modparams.enable_ppgtt;
  1366. struct drm_i915_private *dev_priv;
  1367. struct pci_dev *pdev;
  1368. struct i915_hw_ppgtt *ppgtt;
  1369. int err;
  1370. dev_priv = mock_gem_device();
  1371. if (!dev_priv)
  1372. return -ENOMEM;
  1373. /* Pretend to be a device which supports the 48b PPGTT */
  1374. i915_modparams.enable_ppgtt = 3;
  1375. pdev = dev_priv->drm.pdev;
  1376. dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(39));
  1377. mutex_lock(&dev_priv->drm.struct_mutex);
  1378. ppgtt = i915_ppgtt_create(dev_priv, ERR_PTR(-ENODEV));
  1379. if (IS_ERR(ppgtt)) {
  1380. err = PTR_ERR(ppgtt);
  1381. goto out_unlock;
  1382. }
  1383. if (!i915_vm_is_48bit(&ppgtt->vm)) {
  1384. pr_err("failed to create 48b PPGTT\n");
  1385. err = -EINVAL;
  1386. goto out_close;
  1387. }
  1388. /* If we were ever hit this then it's time to mock the 64K scratch */
  1389. if (!i915_vm_has_scratch_64K(&ppgtt->vm)) {
  1390. pr_err("PPGTT missing 64K scratch page\n");
  1391. err = -EINVAL;
  1392. goto out_close;
  1393. }
  1394. err = i915_subtests(tests, ppgtt);
  1395. out_close:
  1396. i915_ppgtt_close(&ppgtt->vm);
  1397. i915_ppgtt_put(ppgtt);
  1398. out_unlock:
  1399. mutex_unlock(&dev_priv->drm.struct_mutex);
  1400. i915_modparams.enable_ppgtt = saved_ppgtt;
  1401. drm_dev_put(&dev_priv->drm);
  1402. return err;
  1403. }
  1404. int i915_gem_huge_page_live_selftests(struct drm_i915_private *dev_priv)
  1405. {
  1406. static const struct i915_subtest tests[] = {
  1407. SUBTEST(igt_shrink_thp),
  1408. SUBTEST(igt_ppgtt_pin_update),
  1409. SUBTEST(igt_tmpfs_fallback),
  1410. SUBTEST(igt_ppgtt_exhaust_huge),
  1411. SUBTEST(igt_ppgtt_gemfs_huge),
  1412. SUBTEST(igt_ppgtt_internal_huge),
  1413. };
  1414. struct drm_file *file;
  1415. struct i915_gem_context *ctx;
  1416. int err;
  1417. if (!USES_PPGTT(dev_priv)) {
  1418. pr_info("PPGTT not supported, skipping live-selftests\n");
  1419. return 0;
  1420. }
  1421. if (i915_terminally_wedged(&dev_priv->gpu_error))
  1422. return 0;
  1423. file = mock_file(dev_priv);
  1424. if (IS_ERR(file))
  1425. return PTR_ERR(file);
  1426. mutex_lock(&dev_priv->drm.struct_mutex);
  1427. intel_runtime_pm_get(dev_priv);
  1428. ctx = live_context(dev_priv, file);
  1429. if (IS_ERR(ctx)) {
  1430. err = PTR_ERR(ctx);
  1431. goto out_unlock;
  1432. }
  1433. if (ctx->ppgtt)
  1434. ctx->ppgtt->vm.scrub_64K = true;
  1435. err = i915_subtests(tests, ctx);
  1436. out_unlock:
  1437. intel_runtime_pm_put(dev_priv);
  1438. mutex_unlock(&dev_priv->drm.struct_mutex);
  1439. mock_file_free(dev_priv, file);
  1440. return err;
  1441. }