huge_pages.c 39 KB

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