1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <drm/drmP.h>
25#include "amdgpu.h"
26#include "amdgpu_ih.h"
27
28
29
30
31
32
33
34
35
36
37
38
39
40int amdgpu_ih_ring_init(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih,
41 unsigned ring_size, bool use_bus_addr)
42{
43 u32 rb_bufsz;
44 int r;
45
46
47 rb_bufsz = order_base_2(ring_size / 4);
48 ring_size = (1 << rb_bufsz) * 4;
49 ih->ring_size = ring_size;
50 ih->ptr_mask = ih->ring_size - 1;
51 ih->rptr = 0;
52 ih->use_bus_addr = use_bus_addr;
53
54 if (use_bus_addr) {
55 if (ih->ring)
56 return 0;
57
58
59
60
61 ih->ring = dma_alloc_coherent(adev->dev, ih->ring_size + 8,
62 &ih->rb_dma_addr, GFP_KERNEL);
63 if (ih->ring == NULL)
64 return -ENOMEM;
65
66 memset((void *)ih->ring, 0, ih->ring_size + 8);
67 ih->wptr_offs = (ih->ring_size / 4) + 0;
68 ih->rptr_offs = (ih->ring_size / 4) + 1;
69 } else {
70 r = amdgpu_device_wb_get(adev, &ih->wptr_offs);
71 if (r)
72 return r;
73
74 r = amdgpu_device_wb_get(adev, &ih->rptr_offs);
75 if (r) {
76 amdgpu_device_wb_free(adev, ih->wptr_offs);
77 return r;
78 }
79
80 r = amdgpu_bo_create_kernel(adev, ih->ring_size, PAGE_SIZE,
81 AMDGPU_GEM_DOMAIN_GTT,
82 &ih->ring_obj, &ih->gpu_addr,
83 (void **)&ih->ring);
84 if (r) {
85 amdgpu_device_wb_free(adev, ih->rptr_offs);
86 amdgpu_device_wb_free(adev, ih->wptr_offs);
87 return r;
88 }
89 }
90 return 0;
91}
92
93
94
95
96
97
98
99
100
101
102void amdgpu_ih_ring_fini(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih)
103{
104 if (ih->use_bus_addr) {
105 if (!ih->ring)
106 return;
107
108
109
110
111 dma_free_coherent(adev->dev, ih->ring_size + 8,
112 (void *)ih->ring, ih->rb_dma_addr);
113 ih->ring = NULL;
114 } else {
115 amdgpu_bo_free_kernel(&ih->ring_obj, &ih->gpu_addr,
116 (void **)&ih->ring);
117 amdgpu_device_wb_free(adev, ih->wptr_offs);
118 amdgpu_device_wb_free(adev, ih->rptr_offs);
119 }
120}
121
122
123
124
125
126
127
128
129
130
131int amdgpu_ih_process(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih,
132 void (*callback)(struct amdgpu_device *adev,
133 struct amdgpu_ih_ring *ih))
134{
135 u32 wptr;
136
137 if (!ih->enabled || adev->shutdown)
138 return IRQ_NONE;
139
140 wptr = amdgpu_ih_get_wptr(adev);
141
142restart_ih:
143
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
150 rmb();
151
152 while (ih->rptr != wptr) {
153 callback(adev, ih);
154 ih->rptr &= ih->ptr_mask;
155 }
156
157 amdgpu_ih_set_rptr(adev);
158 atomic_set(&ih->lock, 0);
159
160
161 wptr = amdgpu_ih_get_wptr(adev);
162 if (wptr != ih->rptr)
163 goto restart_ih;
164
165 return IRQ_HANDLED;
166}
167
168