i915_gem_object.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Copyright © 2016 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 "mock_gem_device.h"
  26. #include "huge_gem_object.h"
  27. static int igt_gem_object(void *arg)
  28. {
  29. struct drm_i915_private *i915 = arg;
  30. struct drm_i915_gem_object *obj;
  31. int err = -ENOMEM;
  32. /* Basic test to ensure we can create an object */
  33. obj = i915_gem_object_create(i915, PAGE_SIZE);
  34. if (IS_ERR(obj)) {
  35. err = PTR_ERR(obj);
  36. pr_err("i915_gem_object_create failed, err=%d\n", err);
  37. goto out;
  38. }
  39. err = 0;
  40. i915_gem_object_put(obj);
  41. out:
  42. return err;
  43. }
  44. static int igt_phys_object(void *arg)
  45. {
  46. struct drm_i915_private *i915 = arg;
  47. struct drm_i915_gem_object *obj;
  48. int err;
  49. /* Create an object and bind it to a contiguous set of physical pages,
  50. * i.e. exercise the i915_gem_object_phys API.
  51. */
  52. obj = i915_gem_object_create(i915, PAGE_SIZE);
  53. if (IS_ERR(obj)) {
  54. err = PTR_ERR(obj);
  55. pr_err("i915_gem_object_create failed, err=%d\n", err);
  56. goto out;
  57. }
  58. mutex_lock(&i915->drm.struct_mutex);
  59. err = i915_gem_object_attach_phys(obj, PAGE_SIZE);
  60. mutex_unlock(&i915->drm.struct_mutex);
  61. if (err) {
  62. pr_err("i915_gem_object_attach_phys failed, err=%d\n", err);
  63. goto out_obj;
  64. }
  65. if (obj->ops != &i915_gem_phys_ops) {
  66. pr_err("i915_gem_object_attach_phys did not create a phys object\n");
  67. err = -EINVAL;
  68. goto out_obj;
  69. }
  70. if (!atomic_read(&obj->mm.pages_pin_count)) {
  71. pr_err("i915_gem_object_attach_phys did not pin its phys pages\n");
  72. err = -EINVAL;
  73. goto out_obj;
  74. }
  75. /* Make the object dirty so that put_pages must do copy back the data */
  76. mutex_lock(&i915->drm.struct_mutex);
  77. err = i915_gem_object_set_to_gtt_domain(obj, true);
  78. mutex_unlock(&i915->drm.struct_mutex);
  79. if (err) {
  80. pr_err("i915_gem_object_set_to_gtt_domain failed with err=%d\n",
  81. err);
  82. goto out_obj;
  83. }
  84. out_obj:
  85. i915_gem_object_put(obj);
  86. out:
  87. return err;
  88. }
  89. static int igt_gem_huge(void *arg)
  90. {
  91. const unsigned int nreal = 509; /* just to be awkward */
  92. struct drm_i915_private *i915 = arg;
  93. struct drm_i915_gem_object *obj;
  94. unsigned int n;
  95. int err;
  96. /* Basic sanitycheck of our huge fake object allocation */
  97. obj = huge_gem_object(i915,
  98. nreal * PAGE_SIZE,
  99. i915->ggtt.base.total + PAGE_SIZE);
  100. if (IS_ERR(obj))
  101. return PTR_ERR(obj);
  102. err = i915_gem_object_pin_pages(obj);
  103. if (err) {
  104. pr_err("Failed to allocate %u pages (%lu total), err=%d\n",
  105. nreal, obj->base.size / PAGE_SIZE, err);
  106. goto out;
  107. }
  108. for (n = 0; n < obj->base.size / PAGE_SIZE; n++) {
  109. if (i915_gem_object_get_page(obj, n) !=
  110. i915_gem_object_get_page(obj, n % nreal)) {
  111. pr_err("Page lookup mismatch at index %u [%u]\n",
  112. n, n % nreal);
  113. err = -EINVAL;
  114. goto out_unpin;
  115. }
  116. }
  117. out_unpin:
  118. i915_gem_object_unpin_pages(obj);
  119. out:
  120. i915_gem_object_put(obj);
  121. return err;
  122. }
  123. struct tile {
  124. unsigned int width;
  125. unsigned int height;
  126. unsigned int stride;
  127. unsigned int size;
  128. unsigned int tiling;
  129. unsigned int swizzle;
  130. };
  131. static u64 swizzle_bit(unsigned int bit, u64 offset)
  132. {
  133. return (offset & BIT_ULL(bit)) >> (bit - 6);
  134. }
  135. static u64 tiled_offset(const struct tile *tile, u64 v)
  136. {
  137. u64 x, y;
  138. if (tile->tiling == I915_TILING_NONE)
  139. return v;
  140. y = div64_u64_rem(v, tile->stride, &x);
  141. v = div64_u64_rem(y, tile->height, &y) * tile->stride * tile->height;
  142. if (tile->tiling == I915_TILING_X) {
  143. v += y * tile->width;
  144. v += div64_u64_rem(x, tile->width, &x) << tile->size;
  145. v += x;
  146. } else {
  147. const unsigned int ytile_span = 16;
  148. const unsigned int ytile_height = 32 * ytile_span;
  149. v += y * ytile_span;
  150. v += div64_u64_rem(x, ytile_span, &x) * ytile_height;
  151. v += x;
  152. }
  153. switch (tile->swizzle) {
  154. case I915_BIT_6_SWIZZLE_9:
  155. v ^= swizzle_bit(9, v);
  156. break;
  157. case I915_BIT_6_SWIZZLE_9_10:
  158. v ^= swizzle_bit(9, v) ^ swizzle_bit(10, v);
  159. break;
  160. case I915_BIT_6_SWIZZLE_9_11:
  161. v ^= swizzle_bit(9, v) ^ swizzle_bit(11, v);
  162. break;
  163. case I915_BIT_6_SWIZZLE_9_10_11:
  164. v ^= swizzle_bit(9, v) ^ swizzle_bit(10, v) ^ swizzle_bit(11, v);
  165. break;
  166. }
  167. return v;
  168. }
  169. static int check_partial_mapping(struct drm_i915_gem_object *obj,
  170. const struct tile *tile,
  171. unsigned long end_time)
  172. {
  173. const unsigned int nreal = obj->scratch / PAGE_SIZE;
  174. const unsigned long npages = obj->base.size / PAGE_SIZE;
  175. struct i915_vma *vma;
  176. unsigned long page;
  177. int err;
  178. if (igt_timeout(end_time,
  179. "%s: timed out before tiling=%d stride=%d\n",
  180. __func__, tile->tiling, tile->stride))
  181. return -EINTR;
  182. err = i915_gem_object_set_tiling(obj, tile->tiling, tile->stride);
  183. if (err) {
  184. pr_err("Failed to set tiling mode=%u, stride=%u, err=%d\n",
  185. tile->tiling, tile->stride, err);
  186. return err;
  187. }
  188. GEM_BUG_ON(i915_gem_object_get_tiling(obj) != tile->tiling);
  189. GEM_BUG_ON(i915_gem_object_get_stride(obj) != tile->stride);
  190. for_each_prime_number_from(page, 1, npages) {
  191. struct i915_ggtt_view view =
  192. compute_partial_view(obj, page, MIN_CHUNK_PAGES);
  193. u32 __iomem *io;
  194. struct page *p;
  195. unsigned int n;
  196. u64 offset;
  197. u32 *cpu;
  198. GEM_BUG_ON(view.partial.size > nreal);
  199. err = i915_gem_object_set_to_gtt_domain(obj, true);
  200. if (err) {
  201. pr_err("Failed to flush to GTT write domain; err=%d\n",
  202. err);
  203. return err;
  204. }
  205. vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
  206. if (IS_ERR(vma)) {
  207. pr_err("Failed to pin partial view: offset=%lu; err=%d\n",
  208. page, (int)PTR_ERR(vma));
  209. return PTR_ERR(vma);
  210. }
  211. n = page - view.partial.offset;
  212. GEM_BUG_ON(n >= view.partial.size);
  213. io = i915_vma_pin_iomap(vma);
  214. i915_vma_unpin(vma);
  215. if (IS_ERR(io)) {
  216. pr_err("Failed to iomap partial view: offset=%lu; err=%d\n",
  217. page, (int)PTR_ERR(io));
  218. return PTR_ERR(io);
  219. }
  220. iowrite32(page, io + n * PAGE_SIZE/sizeof(*io));
  221. i915_vma_unpin_iomap(vma);
  222. offset = tiled_offset(tile, page << PAGE_SHIFT);
  223. if (offset >= obj->base.size)
  224. continue;
  225. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  226. p = i915_gem_object_get_page(obj, offset >> PAGE_SHIFT);
  227. cpu = kmap(p) + offset_in_page(offset);
  228. drm_clflush_virt_range(cpu, sizeof(*cpu));
  229. if (*cpu != (u32)page) {
  230. pr_err("Partial view for %lu [%u] (offset=%llu, size=%u [%llu, row size %u], fence=%d, tiling=%d, stride=%d) misalignment, expected write to page (%llu + %u [0x%llx]) of 0x%x, found 0x%x\n",
  231. page, n,
  232. view.partial.offset,
  233. view.partial.size,
  234. vma->size >> PAGE_SHIFT,
  235. tile_row_pages(obj),
  236. vma->fence ? vma->fence->id : -1, tile->tiling, tile->stride,
  237. offset >> PAGE_SHIFT,
  238. (unsigned int)offset_in_page(offset),
  239. offset,
  240. (u32)page, *cpu);
  241. err = -EINVAL;
  242. }
  243. *cpu = 0;
  244. drm_clflush_virt_range(cpu, sizeof(*cpu));
  245. kunmap(p);
  246. if (err)
  247. return err;
  248. }
  249. return 0;
  250. }
  251. static int igt_partial_tiling(void *arg)
  252. {
  253. const unsigned int nreal = 1 << 12; /* largest tile row x2 */
  254. struct drm_i915_private *i915 = arg;
  255. struct drm_i915_gem_object *obj;
  256. int tiling;
  257. int err;
  258. /* We want to check the page mapping and fencing of a large object
  259. * mmapped through the GTT. The object we create is larger than can
  260. * possibly be mmaped as a whole, and so we must use partial GGTT vma.
  261. * We then check that a write through each partial GGTT vma ends up
  262. * in the right set of pages within the object, and with the expected
  263. * tiling, which we verify by manual swizzling.
  264. */
  265. obj = huge_gem_object(i915,
  266. nreal << PAGE_SHIFT,
  267. (1 + next_prime_number(i915->ggtt.base.total >> PAGE_SHIFT)) << PAGE_SHIFT);
  268. if (IS_ERR(obj))
  269. return PTR_ERR(obj);
  270. err = i915_gem_object_pin_pages(obj);
  271. if (err) {
  272. pr_err("Failed to allocate %u pages (%lu total), err=%d\n",
  273. nreal, obj->base.size / PAGE_SIZE, err);
  274. goto out;
  275. }
  276. mutex_lock(&i915->drm.struct_mutex);
  277. intel_runtime_pm_get(i915);
  278. if (1) {
  279. IGT_TIMEOUT(end);
  280. struct tile tile;
  281. tile.height = 1;
  282. tile.width = 1;
  283. tile.size = 0;
  284. tile.stride = 0;
  285. tile.swizzle = I915_BIT_6_SWIZZLE_NONE;
  286. tile.tiling = I915_TILING_NONE;
  287. err = check_partial_mapping(obj, &tile, end);
  288. if (err && err != -EINTR)
  289. goto out_unlock;
  290. }
  291. for (tiling = I915_TILING_X; tiling <= I915_TILING_Y; tiling++) {
  292. IGT_TIMEOUT(end);
  293. unsigned int max_pitch;
  294. unsigned int pitch;
  295. struct tile tile;
  296. tile.tiling = tiling;
  297. switch (tiling) {
  298. case I915_TILING_X:
  299. tile.swizzle = i915->mm.bit_6_swizzle_x;
  300. break;
  301. case I915_TILING_Y:
  302. tile.swizzle = i915->mm.bit_6_swizzle_y;
  303. break;
  304. }
  305. if (tile.swizzle == I915_BIT_6_SWIZZLE_UNKNOWN ||
  306. tile.swizzle == I915_BIT_6_SWIZZLE_9_10_17)
  307. continue;
  308. if (INTEL_GEN(i915) <= 2) {
  309. tile.height = 16;
  310. tile.width = 128;
  311. tile.size = 11;
  312. } else if (tile.tiling == I915_TILING_Y &&
  313. HAS_128_BYTE_Y_TILING(i915)) {
  314. tile.height = 32;
  315. tile.width = 128;
  316. tile.size = 12;
  317. } else {
  318. tile.height = 8;
  319. tile.width = 512;
  320. tile.size = 12;
  321. }
  322. if (INTEL_GEN(i915) < 4)
  323. max_pitch = 8192 / tile.width;
  324. else if (INTEL_GEN(i915) < 7)
  325. max_pitch = 128 * I965_FENCE_MAX_PITCH_VAL / tile.width;
  326. else
  327. max_pitch = 128 * GEN7_FENCE_MAX_PITCH_VAL / tile.width;
  328. for (pitch = max_pitch; pitch; pitch >>= 1) {
  329. tile.stride = tile.width * pitch;
  330. err = check_partial_mapping(obj, &tile, end);
  331. if (err == -EINTR)
  332. goto next_tiling;
  333. if (err)
  334. goto out_unlock;
  335. if (pitch > 2 && INTEL_GEN(i915) >= 4) {
  336. tile.stride = tile.width * (pitch - 1);
  337. err = check_partial_mapping(obj, &tile, end);
  338. if (err == -EINTR)
  339. goto next_tiling;
  340. if (err)
  341. goto out_unlock;
  342. }
  343. if (pitch < max_pitch && INTEL_GEN(i915) >= 4) {
  344. tile.stride = tile.width * (pitch + 1);
  345. err = check_partial_mapping(obj, &tile, end);
  346. if (err == -EINTR)
  347. goto next_tiling;
  348. if (err)
  349. goto out_unlock;
  350. }
  351. }
  352. if (INTEL_GEN(i915) >= 4) {
  353. for_each_prime_number(pitch, max_pitch) {
  354. tile.stride = tile.width * pitch;
  355. err = check_partial_mapping(obj, &tile, end);
  356. if (err == -EINTR)
  357. goto next_tiling;
  358. if (err)
  359. goto out_unlock;
  360. }
  361. }
  362. next_tiling: ;
  363. }
  364. out_unlock:
  365. intel_runtime_pm_put(i915);
  366. mutex_unlock(&i915->drm.struct_mutex);
  367. i915_gem_object_unpin_pages(obj);
  368. out:
  369. i915_gem_object_put(obj);
  370. return err;
  371. }
  372. static int make_obj_busy(struct drm_i915_gem_object *obj)
  373. {
  374. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  375. struct i915_request *rq;
  376. struct i915_vma *vma;
  377. int err;
  378. vma = i915_vma_instance(obj, &i915->ggtt.base, NULL);
  379. if (IS_ERR(vma))
  380. return PTR_ERR(vma);
  381. err = i915_vma_pin(vma, 0, 0, PIN_USER);
  382. if (err)
  383. return err;
  384. rq = i915_request_alloc(i915->engine[RCS], i915->kernel_context);
  385. if (IS_ERR(rq)) {
  386. i915_vma_unpin(vma);
  387. return PTR_ERR(rq);
  388. }
  389. i915_vma_move_to_active(vma, rq, 0);
  390. i915_request_add(rq);
  391. i915_gem_object_set_active_reference(obj);
  392. i915_vma_unpin(vma);
  393. return 0;
  394. }
  395. static bool assert_mmap_offset(struct drm_i915_private *i915,
  396. unsigned long size,
  397. int expected)
  398. {
  399. struct drm_i915_gem_object *obj;
  400. int err;
  401. obj = i915_gem_object_create_internal(i915, size);
  402. if (IS_ERR(obj))
  403. return PTR_ERR(obj);
  404. err = i915_gem_object_create_mmap_offset(obj);
  405. i915_gem_object_put(obj);
  406. return err == expected;
  407. }
  408. static int igt_mmap_offset_exhaustion(void *arg)
  409. {
  410. struct drm_i915_private *i915 = arg;
  411. struct drm_mm *mm = &i915->drm.vma_offset_manager->vm_addr_space_mm;
  412. struct drm_i915_gem_object *obj;
  413. struct drm_mm_node resv, *hole;
  414. u64 hole_start, hole_end;
  415. int loop, err;
  416. /* Trim the device mmap space to only a page */
  417. memset(&resv, 0, sizeof(resv));
  418. drm_mm_for_each_hole(hole, mm, hole_start, hole_end) {
  419. resv.start = hole_start;
  420. resv.size = hole_end - hole_start - 1; /* PAGE_SIZE units */
  421. err = drm_mm_reserve_node(mm, &resv);
  422. if (err) {
  423. pr_err("Failed to trim VMA manager, err=%d\n", err);
  424. return err;
  425. }
  426. break;
  427. }
  428. /* Just fits! */
  429. if (!assert_mmap_offset(i915, PAGE_SIZE, 0)) {
  430. pr_err("Unable to insert object into single page hole\n");
  431. err = -EINVAL;
  432. goto out;
  433. }
  434. /* Too large */
  435. if (!assert_mmap_offset(i915, 2*PAGE_SIZE, -ENOSPC)) {
  436. pr_err("Unexpectedly succeeded in inserting too large object into single page hole\n");
  437. err = -EINVAL;
  438. goto out;
  439. }
  440. /* Fill the hole, further allocation attempts should then fail */
  441. obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
  442. if (IS_ERR(obj)) {
  443. err = PTR_ERR(obj);
  444. goto out;
  445. }
  446. err = i915_gem_object_create_mmap_offset(obj);
  447. if (err) {
  448. pr_err("Unable to insert object into reclaimed hole\n");
  449. goto err_obj;
  450. }
  451. if (!assert_mmap_offset(i915, PAGE_SIZE, -ENOSPC)) {
  452. pr_err("Unexpectedly succeeded in inserting object into no holes!\n");
  453. err = -EINVAL;
  454. goto err_obj;
  455. }
  456. i915_gem_object_put(obj);
  457. /* Now fill with busy dead objects that we expect to reap */
  458. for (loop = 0; loop < 3; loop++) {
  459. obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
  460. if (IS_ERR(obj)) {
  461. err = PTR_ERR(obj);
  462. goto out;
  463. }
  464. mutex_lock(&i915->drm.struct_mutex);
  465. intel_runtime_pm_get(i915);
  466. err = make_obj_busy(obj);
  467. intel_runtime_pm_put(i915);
  468. mutex_unlock(&i915->drm.struct_mutex);
  469. if (err) {
  470. pr_err("[loop %d] Failed to busy the object\n", loop);
  471. goto err_obj;
  472. }
  473. GEM_BUG_ON(!i915_gem_object_is_active(obj));
  474. err = i915_gem_object_create_mmap_offset(obj);
  475. if (err) {
  476. pr_err("[loop %d] i915_gem_object_create_mmap_offset failed with err=%d\n",
  477. loop, err);
  478. goto out;
  479. }
  480. }
  481. out:
  482. drm_mm_remove_node(&resv);
  483. return err;
  484. err_obj:
  485. i915_gem_object_put(obj);
  486. goto out;
  487. }
  488. int i915_gem_object_mock_selftests(void)
  489. {
  490. static const struct i915_subtest tests[] = {
  491. SUBTEST(igt_gem_object),
  492. SUBTEST(igt_phys_object),
  493. };
  494. struct drm_i915_private *i915;
  495. int err;
  496. i915 = mock_gem_device();
  497. if (!i915)
  498. return -ENOMEM;
  499. err = i915_subtests(tests, i915);
  500. drm_dev_unref(&i915->drm);
  501. return err;
  502. }
  503. int i915_gem_object_live_selftests(struct drm_i915_private *i915)
  504. {
  505. static const struct i915_subtest tests[] = {
  506. SUBTEST(igt_gem_huge),
  507. SUBTEST(igt_partial_tiling),
  508. SUBTEST(igt_mmap_offset_exhaustion),
  509. };
  510. return i915_subtests(tests, i915);
  511. }