Subversion Repositories Kolibri OS

Rev

Rev 1428 | Rev 1963 | 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.         int r = 0, i, c;
  48.  
  49.         *ib = NULL;
  50.         r = radeon_fence_create(rdev, &fence);
  51.         if (r) {
  52.                 dev_err(rdev->dev, "failed to create fence for new IB\n");
  53.                 return r;
  54.         }
  55.     mutex_lock(&rdev->ib_pool.mutex);
  56.         for (i = rdev->ib_pool.head_id, c = 0, nib = NULL; c < RADEON_IB_POOL_SIZE; c++, i++) {
  57.                 i &= (RADEON_IB_POOL_SIZE - 1);
  58.                 if (rdev->ib_pool.ibs[i].free) {
  59.                         nib = &rdev->ib_pool.ibs[i];
  60.                         break;
  61.         }
  62.         }
  63.         if (nib == NULL) {
  64.                 /* This should never happen, it means we allocated all
  65.                  * IB and haven't scheduled one yet, return EBUSY to
  66.                  * userspace hoping that on ioctl recall we get better
  67.                  * luck
  68.                  */
  69.                 dev_err(rdev->dev, "no free indirect buffer !\n");
  70.                 mutex_unlock(&rdev->ib_pool.mutex);
  71.                 radeon_fence_unref(&fence);
  72.                 return -EBUSY;
  73.         }
  74.         rdev->ib_pool.head_id = (nib->idx + 1) & (RADEON_IB_POOL_SIZE - 1);
  75.         nib->free = false;
  76.         if (nib->fence) {
  77.                 mutex_unlock(&rdev->ib_pool.mutex);
  78.         r = radeon_fence_wait(nib->fence, false);
  79.         if (r) {
  80.                         dev_err(rdev->dev, "error waiting fence of IB(%u:0x%016lX:%u)\n",
  81.                                 nib->idx, (unsigned long)nib->gpu_addr, nib->length_dw);
  82.                         mutex_lock(&rdev->ib_pool.mutex);
  83.                         nib->free = true;
  84.                         mutex_unlock(&rdev->ib_pool.mutex);
  85.                         radeon_fence_unref(&fence);
  86.                         return r;
  87.                 }
  88.                 mutex_lock(&rdev->ib_pool.mutex);
  89.         }
  90.         radeon_fence_unref(&nib->fence);
  91.         nib->fence = fence;
  92.         nib->length_dw = 0;
  93.         mutex_unlock(&rdev->ib_pool.mutex);
  94.         *ib = nib;
  95.         return 0;
  96. }
  97.  
  98. void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
  99. {
  100.         struct radeon_ib *tmp = *ib;
  101.  
  102.         *ib = NULL;
  103.         if (tmp == NULL) {
  104.                 return;
  105.         }
  106.         if (!tmp->fence->emited)
  107.                 radeon_fence_unref(&tmp->fence);
  108.         mutex_lock(&rdev->ib_pool.mutex);
  109.         tmp->free = true;
  110.         mutex_unlock(&rdev->ib_pool.mutex);
  111. }
  112.  
  113. int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
  114. {
  115.         int r = 0;
  116.  
  117.         if (!ib->length_dw || !rdev->cp.ready) {
  118.                 /* TODO: Nothings in the ib we should report. */
  119.                 DRM_ERROR("radeon: couldn't schedule IB(%u).\n", ib->idx);
  120.                 return -EINVAL;
  121.         }
  122.  
  123.         /* 64 dwords should be enough for fence too */
  124.         r = radeon_ring_lock(rdev, 64);
  125.         if (r) {
  126.                 DRM_ERROR("radeon: scheduling IB failled (%d).\n", r);
  127.                 return r;
  128.         }
  129.         radeon_ring_ib_execute(rdev, ib);
  130.         radeon_fence_emit(rdev, ib->fence);
  131.         mutex_lock(&rdev->ib_pool.mutex);
  132.         /* once scheduled IB is considered free and protected by the fence */
  133.         ib->free = true;
  134.         mutex_unlock(&rdev->ib_pool.mutex);
  135.         radeon_ring_unlock_commit(rdev);
  136.         return 0;
  137. }
  138. #endif
  139.  
  140. int radeon_ib_pool_init(struct radeon_device *rdev)
  141. {
  142.         void *ptr;
  143.         uint64_t gpu_addr;
  144.         int i;
  145.         int r = 0;
  146.  
  147.         if (rdev->ib_pool.robj)
  148.                 return 0;
  149.         INIT_LIST_HEAD(&rdev->ib_pool.bogus_ib);
  150.         /* Allocate 1M object buffer */
  151.         r = radeon_bo_create(rdev, NULL,  RADEON_IB_POOL_SIZE*64*1024,
  152.                                  true, RADEON_GEM_DOMAIN_GTT,
  153.                                 &rdev->ib_pool.robj);
  154.         if (r) {
  155.                 DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
  156.                 return r;
  157.         }
  158.         r = radeon_bo_reserve(rdev->ib_pool.robj, false);
  159.         if (unlikely(r != 0))
  160.                 return r;
  161.         r = radeon_bo_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
  162.         if (r) {
  163.                 radeon_bo_unreserve(rdev->ib_pool.robj);
  164.                 DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
  165.                 return r;
  166.         }
  167.         r = radeon_bo_kmap(rdev->ib_pool.robj, &ptr);
  168.         radeon_bo_unreserve(rdev->ib_pool.robj);
  169.         if (r) {
  170.                 DRM_ERROR("radeon: failed to map ib poll (%d).\n", r);
  171.                 return r;
  172.         }
  173.         for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  174.                 unsigned offset;
  175.  
  176.                 offset = i * 64 * 1024;
  177.                 rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
  178.                 rdev->ib_pool.ibs[i].ptr = ptr + offset;
  179.                 rdev->ib_pool.ibs[i].idx = i;
  180.                 rdev->ib_pool.ibs[i].length_dw = 0;
  181.                 rdev->ib_pool.ibs[i].free = true;
  182.         }
  183.         rdev->ib_pool.head_id = 0;
  184.         rdev->ib_pool.ready = true;
  185.         DRM_INFO("radeon: ib pool ready.\n");
  186.         if (radeon_debugfs_ib_init(rdev)) {
  187.                 DRM_ERROR("Failed to register debugfs file for IB !\n");
  188.         }
  189.         return r;
  190. }
  191.  
  192. void radeon_ib_pool_fini(struct radeon_device *rdev)
  193. {
  194.         int r;
  195.  
  196.         if (!rdev->ib_pool.ready) {
  197.                 return;
  198.         }
  199.         mutex_lock(&rdev->ib_pool.mutex);
  200.         if (rdev->ib_pool.robj) {
  201.                 r = radeon_bo_reserve(rdev->ib_pool.robj, false);
  202.                 if (likely(r == 0)) {
  203.                         radeon_bo_kunmap(rdev->ib_pool.robj);
  204.                         radeon_bo_unpin(rdev->ib_pool.robj);
  205.                         radeon_bo_unreserve(rdev->ib_pool.robj);
  206.                 }
  207.                 radeon_bo_unref(&rdev->ib_pool.robj);
  208.                 rdev->ib_pool.robj = NULL;
  209.         }
  210.         mutex_unlock(&rdev->ib_pool.mutex);
  211. }
  212.  
  213.  
  214. /*
  215.  * Ring.
  216.  */
  217. void radeon_ring_free_size(struct radeon_device *rdev)
  218. {
  219.         if (rdev->family >= CHIP_R600)
  220.                 rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
  221.         else
  222.         rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
  223.         /* This works because ring_size is a power of 2 */
  224.         rdev->cp.ring_free_dw = (rdev->cp.rptr + (rdev->cp.ring_size / 4));
  225.         rdev->cp.ring_free_dw -= rdev->cp.wptr;
  226.         rdev->cp.ring_free_dw &= rdev->cp.ptr_mask;
  227.         if (!rdev->cp.ring_free_dw) {
  228.                 rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  229.         }
  230. }
  231.  
  232. int radeon_ring_lock(struct radeon_device *rdev, unsigned ndw)
  233. {
  234.         int r;
  235.  
  236.         /* Align requested size with padding so unlock_commit can
  237.          * pad safely */
  238.         ndw = (ndw + rdev->cp.align_mask) & ~rdev->cp.align_mask;
  239.         mutex_lock(&rdev->cp.mutex);
  240.         while (ndw > (rdev->cp.ring_free_dw - 1)) {
  241.                 radeon_ring_free_size(rdev);
  242.                 if (ndw < rdev->cp.ring_free_dw) {
  243.                         break;
  244.                 }
  245. //        r = radeon_fence_wait_next(rdev);
  246. //       if (r) {
  247. //           mutex_unlock(&rdev->cp.mutex);
  248. //           return r;
  249. //       }
  250.         }
  251.         rdev->cp.count_dw = ndw;
  252.         rdev->cp.wptr_old = rdev->cp.wptr;
  253.         return 0;
  254. }
  255.  
  256. void radeon_ring_unlock_commit(struct radeon_device *rdev)
  257. {
  258.         unsigned count_dw_pad;
  259.         unsigned i;
  260.  
  261.         /* We pad to match fetch size */
  262.         count_dw_pad = (rdev->cp.align_mask + 1) -
  263.                        (rdev->cp.wptr & rdev->cp.align_mask);
  264.         for (i = 0; i < count_dw_pad; i++) {
  265.                 radeon_ring_write(rdev, 2 << 30);
  266.         }
  267.         DRM_MEMORYBARRIER();
  268.         radeon_cp_commit(rdev);
  269.         mutex_unlock(&rdev->cp.mutex);
  270. }
  271.  
  272. void radeon_ring_unlock_undo(struct radeon_device *rdev)
  273. {
  274.         rdev->cp.wptr = rdev->cp.wptr_old;
  275.         mutex_unlock(&rdev->cp.mutex);
  276. }
  277.  
  278. int radeon_ring_init(struct radeon_device *rdev, unsigned ring_size)
  279. {
  280.         int r;
  281.  
  282.         rdev->cp.ring_size = ring_size;
  283.     /* Allocate ring buffer */
  284.         if (rdev->cp.ring_obj == NULL) {
  285.                 r = radeon_bo_create(rdev, NULL, rdev->cp.ring_size, true,
  286.                                          RADEON_GEM_DOMAIN_GTT,
  287.                                          &rdev->cp.ring_obj);
  288.                 if (r) {
  289.                         dev_err(rdev->dev, "(%d) ring create failed\n", r);
  290.                         return r;
  291.                 }
  292.                 r = radeon_bo_reserve(rdev->cp.ring_obj, false);
  293.                 if (unlikely(r != 0))
  294.                         return r;
  295.                 r = radeon_bo_pin(rdev->cp.ring_obj, RADEON_GEM_DOMAIN_GTT,
  296.                                       &rdev->cp.gpu_addr);
  297.                 if (r) {
  298.                         radeon_bo_unreserve(rdev->cp.ring_obj);
  299.                         dev_err(rdev->dev, "(%d) ring pin failed\n", r);
  300.                         return r;
  301.                 }
  302.                 r = radeon_bo_kmap(rdev->cp.ring_obj,
  303.                                        (void **)&rdev->cp.ring);
  304.                 radeon_bo_unreserve(rdev->cp.ring_obj);
  305.                 if (r) {
  306.                         dev_err(rdev->dev, "(%d) ring map failed\n", r);
  307.                         return r;
  308.                 }
  309.         }
  310.         rdev->cp.ptr_mask = (rdev->cp.ring_size / 4) - 1;
  311.         rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
  312.         return 0;
  313. }
  314.  
  315. void radeon_ring_fini(struct radeon_device *rdev)
  316. {
  317.         int r;
  318.  
  319.         mutex_lock(&rdev->cp.mutex);
  320.    if (rdev->cp.ring_obj) {
  321.                 r = radeon_bo_reserve(rdev->cp.ring_obj, false);
  322.                 if (likely(r == 0)) {
  323.                         radeon_bo_kunmap(rdev->cp.ring_obj);
  324.                         radeon_bo_unpin(rdev->cp.ring_obj);
  325.                         radeon_bo_unreserve(rdev->cp.ring_obj);
  326.                 }
  327.                 radeon_bo_unref(&rdev->cp.ring_obj);
  328.        rdev->cp.ring = NULL;
  329.                 rdev->cp.ring_obj = NULL;
  330.         }
  331.         mutex_unlock(&rdev->cp.mutex);
  332. }
  333.  
  334.  
  335. /*
  336.  * Debugfs info
  337.  */
  338. #if defined(CONFIG_DEBUG_FS)
  339. static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
  340. {
  341.         struct drm_info_node *node = (struct drm_info_node *) m->private;
  342.         struct radeon_ib *ib = node->info_ent->data;
  343.         unsigned i;
  344.  
  345.         if (ib == NULL) {
  346.                 return 0;
  347.         }
  348.         seq_printf(m, "IB %04u\n", ib->idx);
  349.         seq_printf(m, "IB fence %p\n", ib->fence);
  350.         seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
  351.         for (i = 0; i < ib->length_dw; i++) {
  352.                 seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
  353.         }
  354.         return 0;
  355. }
  356.  
  357. static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
  358. static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
  359. #endif
  360.  
  361. int radeon_debugfs_ib_init(struct radeon_device *rdev)
  362. {
  363. #if defined(CONFIG_DEBUG_FS)
  364.         unsigned i;
  365.         int r;
  366.  
  367.         radeon_debugfs_ib_bogus_info_list[0].data = rdev;
  368.         r = radeon_debugfs_add_files(rdev, radeon_debugfs_ib_bogus_info_list, 1);
  369.         if (r)
  370.                 return r;
  371.         for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
  372.                 sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
  373.                 radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
  374.                 radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
  375.                 radeon_debugfs_ib_list[i].driver_features = 0;
  376.                 radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
  377.         }
  378.         return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
  379.                                         RADEON_IB_POOL_SIZE);
  380. #else
  381.         return 0;
  382. #endif
  383. }
  384.