Subversion Repositories Kolibri OS

Rev

Rev 1179 | Rev 1428 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2008 Advanced Micro Devices, Inc.
  3.  * Copyright 2008 Red Hat Inc.
  4.  * Copyright 2009 Jerome Glisse.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in
  14.  * all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22.  * OTHER DEALINGS IN THE SOFTWARE.
  23.  *
  24.  * Authors: Dave Airlie
  25.  *          Alex Deucher
  26.  *          Jerome Glisse
  27.  */
  28. #include <linux/seq_file.h>
  29. #include "drmP.h"
  30. #include "radeon_drm.h"
  31. #include "radeon_reg.h"
  32. #include "radeon.h"
  33. #include "atom.h"
  34.  
  35. int radeon_debugfs_ib_init(struct radeon_device *rdev);
  36.  
  37. /*
  38.  * IB.
  39.  */
  40.  
  41. #if 0
  42.  
  43. int radeon_ib_get(struct radeon_device *rdev, struct radeon_ib **ib)
  44. {
  45.         struct radeon_fence *fence;
  46.         struct radeon_ib *nib;
  47.         unsigned long i;
  48.         int r = 0;
  49.  
  50.         *ib = NULL;
  51.         r = radeon_fence_create(rdev, &fence);
  52.         if (r) {
  53.                 DRM_ERROR("failed to create fence for new IB\n");
  54.                 return r;
  55.         }
  56.     mutex_lock(&rdev->ib_pool.mutex);
  57.         i = find_first_zero_bit(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
  58.         if (i < RADEON_IB_POOL_SIZE) {
  59.                 set_bit(i, rdev->ib_pool.alloc_bm);
  60.                 rdev->ib_pool.ibs[i].length_dw = 0;
  61.                 *ib = &rdev->ib_pool.ibs[i];
  62.                 mutex_unlock(&rdev->ib_pool.mutex);
  63.                 goto out;
  64.         }
  65.         if (list_empty(&rdev->ib_pool.scheduled_ibs)) {
  66.                 /* we go do nothings here */
  67.                 mutex_unlock(&rdev->ib_pool.mutex);
  68.                 DRM_ERROR("all IB allocated none scheduled.\n");
  69.                 r = -EINVAL;
  70.                 goto out;
  71.         }
  72.         /* get the first ib on the scheduled list */
  73.         nib = list_entry(rdev->ib_pool.scheduled_ibs.next,
  74.                          struct radeon_ib, list);
  75.         if (nib->fence == NULL) {
  76.                 /* we go do nothings here */
  77.                 mutex_unlock(&rdev->ib_pool.mutex);
  78.                 DRM_ERROR("IB %lu scheduled without a fence.\n", nib->idx);
  79.                 r = -EINVAL;
  80.                 goto out;
  81.         }
  82.         mutex_unlock(&rdev->ib_pool.mutex);
  83.  
  84.         r = radeon_fence_wait(nib->fence, false);
  85.         if (r) {
  86.                 DRM_ERROR("radeon: IB(%lu:0x%016lX:%u)\n", nib->idx,
  87.                           (unsigned long)nib->gpu_addr, nib->length_dw);
  88.                 DRM_ERROR("radeon: GPU lockup detected, fail to get a IB\n");
  89.                 goto out;
  90.         }
  91.         radeon_fence_unref(&nib->fence);
  92.  
  93.         nib->length_dw = 0;
  94.  
  95.         /* scheduled list is accessed here */
  96.         mutex_lock(&rdev->ib_pool.mutex);
  97.         list_del(&nib->list);
  98.         INIT_LIST_HEAD(&nib->list);
  99.         mutex_unlock(&rdev->ib_pool.mutex);
  100.  
  101.         *ib = nib;
  102. out:
  103.         if (r) {
  104.                 radeon_fence_unref(&fence);
  105.         } else {
  106.                 (*ib)->fence = fence;
  107.         }
  108.         return r;
  109. }
  110.  
  111. void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
  112. {
  113.         struct radeon_ib *tmp = *ib;
  114.  
  115.         *ib = NULL;
  116.         if (tmp == NULL) {
  117.                 return;
  118.         }
  119.         mutex_lock(&rdev->ib_pool.mutex);
  120.         if (!list_empty(&tmp->list) && !radeon_fence_signaled(tmp->fence)) {
  121.                 /* IB is scheduled & not signaled don't do anythings */
  122.                 mutex_unlock(&rdev->ib_pool.mutex);
  123.                 return;
  124.         }
  125.         list_del(&tmp->list);
  126.         INIT_LIST_HEAD(&tmp->list);
  127.         if (tmp->fence)
  128.                 radeon_fence_unref(&tmp->fence);
  129.  
  130.         tmp->length_dw = 0;
  131.         clear_bit(tmp->idx, rdev->ib_pool.alloc_bm);
  132.         mutex_unlock(&rdev->ib_pool.mutex);
  133. }
  134.  
  135. int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
  136. {
  137.         int r = 0;
  138.  
  139.         if (!ib->length_dw || !rdev->cp.ready) {
  140.                 /* TODO: Nothings in the ib we should report. */
  141.                 DRM_ERROR("radeon: couldn't schedule IB(%lu).\n", ib->idx);
  142.                 return -EINVAL;
  143.         }
  144.  
  145.         /* 64 dwords should be enough for fence too */
  146.         r = radeon_ring_lock(rdev, 64);
  147.         if (r) {
  148.                 DRM_ERROR("radeon: scheduling IB failled (%d).\n", r);
  149.                 return r;
  150.         }
  151.         radeon_ring_ib_execute(rdev, ib);
  152.         radeon_fence_emit(rdev, ib->fence);
  153.         mutex_lock(&rdev->ib_pool.mutex);
  154.         list_add_tail(&ib->list, &rdev->ib_pool.scheduled_ibs);
  155.         mutex_unlock(&rdev->ib_pool.mutex);
  156.         radeon_ring_unlock_commit(rdev);
  157.         return 0;
  158. }
  159. #endif
  160.  
  161. int radeon_ib_pool_init(struct radeon_device *rdev)
  162. {
  163.         void *ptr;
  164.         uint64_t gpu_addr;
  165.         int i;
  166.         int r = 0;
  167.  
  168.         if (rdev->ib_pool.robj)
  169.                 return 0;
  170.         /* Allocate 1M object buffer */
  171.         INIT_LIST_HEAD(&rdev->ib_pool.scheduled_ibs);
  172.         r = radeon_bo_create(rdev, NULL,  RADEON_IB_POOL_SIZE*64*1024,
  173.                                  true, RADEON_GEM_DOMAIN_GTT,
  174.                                 &rdev->ib_pool.robj);
  175.         if (r) {
  176.                 DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
  177.                 return r;
  178.         }
  179.         r = radeon_bo_reserve(rdev->ib_pool.robj, false);
  180.         if (unlikely(r != 0))
  181.                 return r;
  182.         r = radeon_bo_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
  183.         if (r) {
  184.                 radeon_bo_unreserve(rdev->ib_pool.robj);
  185.                 DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
  186.                 return r;
  187.         }
  188.         r = radeon_bo_kmap(rdev->ib_pool.robj, &ptr);
  189.         radeon_bo_unreserve(rdev->ib_pool.robj);
  190.         if (r) {
  191.                 DRM_ERROR("radeon: failed to map ib poll (%d).\n", r);
  192.                 return r;
  193.         }
  194.         for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  195.                 unsigned offset;
  196.  
  197.                 offset = i * 64 * 1024;
  198.                 rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
  199.                 rdev->ib_pool.ibs[i].ptr = ptr + offset;
  200.                 rdev->ib_pool.ibs[i].idx = i;
  201.                 rdev->ib_pool.ibs[i].length_dw = 0;
  202.                 INIT_LIST_HEAD(&rdev->ib_pool.ibs[i].list);
  203.         }
  204.         bitmap_zero(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
  205.         rdev->ib_pool.ready = true;
  206.         DRM_INFO("radeon: ib pool ready.\n");
  207.         if (radeon_debugfs_ib_init(rdev)) {
  208.                 DRM_ERROR("Failed to register debugfs file for IB !\n");
  209.         }
  210.         return r;
  211. }
  212.  
  213. void radeon_ib_pool_fini(struct radeon_device *rdev)
  214. {
  215.         int r;
  216.  
  217.         if (!rdev->ib_pool.ready) {
  218.                 return;
  219.         }
  220.         mutex_lock(&rdev->ib_pool.mutex);
  221.         bitmap_zero(rdev->ib_pool.alloc_bm, RADEON_IB_POOL_SIZE);
  222.         if (rdev->ib_pool.robj) {
  223.                 r = radeon_bo_reserve(rdev->ib_pool.robj, false);
  224.                 if (likely(r == 0)) {
  225.                         radeon_bo_kunmap(rdev->ib_pool.robj);
  226.                         radeon_bo_unpin(rdev->ib_pool.robj);
  227.                         radeon_bo_unreserve(rdev->ib_pool.robj);
  228.                 }
  229.                 radeon_bo_unref(&rdev->ib_pool.robj);
  230.                 rdev->ib_pool.robj = NULL;
  231.         }
  232.         mutex_unlock(&rdev->ib_pool.mutex);
  233. }
  234.  
  235.  
  236. /*
  237.  * Ring.
  238.  */
  239. void radeon_ring_free_size(struct radeon_device *rdev)
  240. {
  241.         if (rdev->family >= CHIP_R600)
  242.                 rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
  243.         else
  244.         rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
  245.         /* This works because ring_size is a power of 2 */
  246.         rdev->cp.ring_free_dw = (rdev->cp.rptr + (rdev->cp.ring_size / 4));
  247.         rdev->cp.ring_free_dw -= rdev->cp.wptr;
  248.         rdev->cp.ring_free_dw &= rdev->cp.ptr_mask;
  249.         if (!rdev->cp.ring_free_dw) {
  250.                 rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  251.         }
  252. }
  253.  
  254. int radeon_ring_lock(struct radeon_device *rdev, unsigned ndw)
  255. {
  256.         int r;
  257.  
  258.         /* Align requested size with padding so unlock_commit can
  259.          * pad safely */
  260.         ndw = (ndw + rdev->cp.align_mask) & ~rdev->cp.align_mask;
  261.         mutex_lock(&rdev->cp.mutex);
  262.         while (ndw > (rdev->cp.ring_free_dw - 1)) {
  263.                 radeon_ring_free_size(rdev);
  264.                 if (ndw < rdev->cp.ring_free_dw) {
  265.                         break;
  266.                 }
  267. //        r = radeon_fence_wait_next(rdev);
  268. //       if (r) {
  269. //           mutex_unlock(&rdev->cp.mutex);
  270. //           return r;
  271. //       }
  272.         }
  273.         rdev->cp.count_dw = ndw;
  274.         rdev->cp.wptr_old = rdev->cp.wptr;
  275.         return 0;
  276. }
  277.  
  278. void radeon_ring_unlock_commit(struct radeon_device *rdev)
  279. {
  280.         unsigned count_dw_pad;
  281.         unsigned i;
  282.  
  283.         /* We pad to match fetch size */
  284.         count_dw_pad = (rdev->cp.align_mask + 1) -
  285.                        (rdev->cp.wptr & rdev->cp.align_mask);
  286.         for (i = 0; i < count_dw_pad; i++) {
  287.                 radeon_ring_write(rdev, 2 << 30);
  288.         }
  289.         DRM_MEMORYBARRIER();
  290.         radeon_cp_commit(rdev);
  291.         mutex_unlock(&rdev->cp.mutex);
  292. }
  293.  
  294. void radeon_ring_unlock_undo(struct radeon_device *rdev)
  295. {
  296.         rdev->cp.wptr = rdev->cp.wptr_old;
  297.         mutex_unlock(&rdev->cp.mutex);
  298. }
  299.  
  300. int radeon_ring_init(struct radeon_device *rdev, unsigned ring_size)
  301. {
  302.         int r;
  303.  
  304.     ENTER();
  305.  
  306.         rdev->cp.ring_size = ring_size;
  307.     /* Allocate ring buffer */
  308.         if (rdev->cp.ring_obj == NULL) {
  309.                 r = radeon_bo_create(rdev, NULL, rdev->cp.ring_size, true,
  310.                                          RADEON_GEM_DOMAIN_GTT,
  311.                                          &rdev->cp.ring_obj);
  312.                 if (r) {
  313.                         dev_err(rdev->dev, "(%d) ring create failed\n", r);
  314.                         return r;
  315.                 }
  316.                 r = radeon_bo_reserve(rdev->cp.ring_obj, false);
  317.                 if (unlikely(r != 0))
  318.                         return r;
  319.                 r = radeon_bo_pin(rdev->cp.ring_obj, RADEON_GEM_DOMAIN_GTT,
  320.                                       &rdev->cp.gpu_addr);
  321.                 if (r) {
  322.                         radeon_bo_unreserve(rdev->cp.ring_obj);
  323.                         dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  324.                         return r;
  325.                 }
  326.                 r = radeon_bo_kmap(rdev->cp.ring_obj,
  327.                                        (void **)&rdev->cp.ring);
  328.                 radeon_bo_unreserve(rdev->cp.ring_obj);
  329.                 if (r) {
  330.                         dev_err(rdev->dev, "(%d) ring map failed\n", r);
  331.                         return r;
  332.                 }
  333.         }
  334.         rdev->cp.ptr_mask = (rdev->cp.ring_size / 4) - 1;
  335.         rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  336.  
  337.     LEAVE();
  338.  
  339.         return 0;
  340. }
  341.  
  342. void radeon_ring_fini(struct radeon_device *rdev)
  343. {
  344.         int r;
  345.  
  346.         mutex_lock(&rdev->cp.mutex);
  347.    if (rdev->cp.ring_obj) {
  348.                 r = radeon_bo_reserve(rdev->cp.ring_obj, false);
  349.                 if (likely(r == 0)) {
  350.                         radeon_bo_kunmap(rdev->cp.ring_obj);
  351.                         radeon_bo_unpin(rdev->cp.ring_obj);
  352.                         radeon_bo_unreserve(rdev->cp.ring_obj);
  353.                 }
  354.                 radeon_bo_unref(&rdev->cp.ring_obj);
  355.        rdev->cp.ring = NULL;
  356.                 rdev->cp.ring_obj = NULL;
  357.         }
  358.         mutex_unlock(&rdev->cp.mutex);
  359. }
  360.  
  361.  
  362. /*
  363.  * Debugfs info
  364.  */
  365. #if defined(CONFIG_DEBUG_FS)
  366. static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
  367. {
  368.         struct drm_info_node *node = (struct drm_info_node *) m->private;
  369.         struct radeon_ib *ib = node->info_ent->data;
  370.         unsigned i;
  371.  
  372.         if (ib == NULL) {
  373.                 return 0;
  374.         }
  375.         seq_printf(m, "IB %04lu\n", ib->idx);
  376.         seq_printf(m, "IB fence %p\n", ib->fence);
  377.         seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  378.         for (i = 0; i < ib->length_dw; i++) {
  379.                 seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  380.         }
  381.         return 0;
  382. }
  383.  
  384. static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
  385. static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
  386. #endif
  387.  
  388. int radeon_debugfs_ib_init(struct radeon_device *rdev)
  389. {
  390. #if defined(CONFIG_DEBUG_FS)
  391.         unsigned i;
  392.  
  393.         for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  394.                 sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
  395.                 radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
  396.                 radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
  397.                 radeon_debugfs_ib_list[i].driver_features = 0;
  398.                 radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
  399.         }
  400.         return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
  401.                                         RADEON_IB_POOL_SIZE);
  402. #else
  403.         return 0;
  404. #endif
  405. }
  406.