]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_ih.c
drm/amdgpu: cleanup amdgpu_ih.c
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_ih.c
1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23
24 #include <drm/drmP.h>
25 #include "amdgpu.h"
26 #include "amdgpu_ih.h"
27 #include "amdgpu_amdkfd.h"
28
29 /**
30  * amdgpu_ih_ring_init - initialize the IH state
31  *
32  * @adev: amdgpu_device pointer
33  * @ih: ih ring to initialize
34  * @ring_size: ring size to allocate
35  * @use_bus_addr: true when we can use dma_alloc_coherent
36  *
37  * Initializes the IH state and allocates a buffer
38  * for the IH ring buffer.
39  * Returns 0 for success, errors for failure.
40  */
41 int amdgpu_ih_ring_init(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih,
42                         unsigned ring_size, bool use_bus_addr)
43 {
44         u32 rb_bufsz;
45         int r;
46
47         /* Align ring size */
48         rb_bufsz = order_base_2(ring_size / 4);
49         ring_size = (1 << rb_bufsz) * 4;
50         ih->ring_size = ring_size;
51         ih->ptr_mask = ih->ring_size - 1;
52         ih->rptr = 0;
53         ih->use_bus_addr = use_bus_addr;
54
55         if (use_bus_addr) {
56                 if (ih->ring)
57                         return 0;
58
59                 /* add 8 bytes for the rptr/wptr shadows and
60                  * add them to the end of the ring allocation.
61                  */
62                 ih->ring = dma_alloc_coherent(adev->dev, ih->ring_size + 8,
63                                               &ih->rb_dma_addr, GFP_KERNEL);
64                 if (ih->ring == NULL)
65                         return -ENOMEM;
66
67                 memset((void *)ih->ring, 0, ih->ring_size + 8);
68                 ih->wptr_offs = (ih->ring_size / 4) + 0;
69                 ih->rptr_offs = (ih->ring_size / 4) + 1;
70         } else {
71                 r = amdgpu_device_wb_get(adev, &ih->wptr_offs);
72                 if (r)
73                         return r;
74
75                 r = amdgpu_device_wb_get(adev, &ih->rptr_offs);
76                 if (r) {
77                         amdgpu_device_wb_free(adev, ih->wptr_offs);
78                         return r;
79                 }
80
81                 r = amdgpu_bo_create_kernel(adev, ih->ring_size, PAGE_SIZE,
82                                             AMDGPU_GEM_DOMAIN_GTT,
83                                             &ih->ring_obj, &ih->gpu_addr,
84                                             (void **)&ih->ring);
85                 if (r) {
86                         amdgpu_device_wb_free(adev, ih->rptr_offs);
87                         amdgpu_device_wb_free(adev, ih->wptr_offs);
88                         return r;
89                 }
90         }
91         return 0;
92 }
93
94 /**
95  * amdgpu_ih_ring_fini - tear down the IH state
96  *
97  * @adev: amdgpu_device pointer
98  * @ih: ih ring to tear down
99  *
100  * Tears down the IH state and frees buffer
101  * used for the IH ring buffer.
102  */
103 void amdgpu_ih_ring_fini(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih)
104 {
105         if (ih->use_bus_addr) {
106                 if (!ih->ring)
107                         return;
108
109                 /* add 8 bytes for the rptr/wptr shadows and
110                  * add them to the end of the ring allocation.
111                  */
112                 dma_free_coherent(adev->dev, ih->ring_size + 8,
113                                   (void *)ih->ring, ih->rb_dma_addr);
114                 ih->ring = NULL;
115         } else {
116                 amdgpu_bo_free_kernel(&ih->ring_obj, &ih->gpu_addr,
117                                       (void **)&ih->ring);
118                 amdgpu_device_wb_free(adev, ih->wptr_offs);
119                 amdgpu_device_wb_free(adev, ih->rptr_offs);
120         }
121 }
122
123 /**
124  * amdgpu_ih_process - interrupt handler
125  *
126  * @adev: amdgpu_device pointer
127  * @ih: ih ring to process
128  *
129  * Interrupt hander (VI), walk the IH ring.
130  * Returns irq process return code.
131  */
132 int amdgpu_ih_process(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih)
133 {
134         struct amdgpu_iv_entry entry;
135         u32 wptr;
136
137         if (!ih->enabled || adev->shutdown)
138                 return IRQ_NONE;
139
140         wptr = amdgpu_ih_get_wptr(adev);
141
142 restart_ih:
143         /* is somebody else already processing irqs? */
144         if (atomic_xchg(&ih->lock, 1))
145                 return IRQ_NONE;
146
147         DRM_DEBUG("%s: rptr %d, wptr %d\n", __func__, ih->rptr, wptr);
148
149         /* Order reading of wptr vs. reading of IH ring data */
150         rmb();
151
152         while (ih->rptr != wptr) {
153                 u32 ring_index = ih->rptr >> 2;
154
155                 /* Prescreening of high-frequency interrupts */
156                 if (!amdgpu_ih_prescreen_iv(adev)) {
157                         ih->rptr &= ih->ptr_mask;
158                         continue;
159                 }
160
161                 /* Before dispatching irq to IP blocks, send it to amdkfd */
162                 amdgpu_amdkfd_interrupt(adev,
163                                         (const void *) &ih->ring[ring_index]);
164
165                 entry.iv_entry = (const uint32_t *)&ih->ring[ring_index];
166                 amdgpu_ih_decode_iv(adev, &entry);
167                 ih->rptr &= ih->ptr_mask;
168
169                 amdgpu_irq_dispatch(adev, &entry);
170         }
171         amdgpu_ih_set_rptr(adev);
172         atomic_set(&ih->lock, 0);
173
174         /* make sure wptr hasn't changed while processing */
175         wptr = amdgpu_ih_get_wptr(adev);
176         if (wptr != ih->rptr)
177                 goto restart_ih;
178
179         return IRQ_HANDLED;
180 }
181
This page took 0.044957 seconds and 4 git commands to generate.