blob: a7afe553e0a165361e5be1a2cdee46c28ed1e92f [file] [log] [blame]
Chunming Zhoud03846a2015-07-28 14:20:03 -04001/*
2 * Copyright 2015 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 */
Chunming Zhou57ff96c2015-04-24 17:38:20 +080024#include <linux/list.h>
25#include <linux/slab.h>
Chunming Zhou97cb7f62015-05-22 11:33:31 -040026#include <linux/pci.h>
Rex Zhu3f1d35a2015-09-15 14:44:44 +080027#include <linux/acpi.h>
Chunming Zhou57ff96c2015-04-24 17:38:20 +080028#include <drm/drmP.h>
Jammy Zhoubf3911b02015-05-13 18:58:05 +080029#include <linux/firmware.h>
Chunming Zhou57ff96c2015-04-24 17:38:20 +080030#include <drm/amdgpu_drm.h>
Chunming Zhoud03846a2015-07-28 14:20:03 -040031#include "amdgpu.h"
32#include "cgs_linux.h"
Chunming Zhou25da4422015-05-22 12:14:04 -040033#include "atom.h"
Jammy Zhoubf3911b02015-05-13 18:58:05 +080034#include "amdgpu_ucode.h"
35
Chunming Zhoud03846a2015-07-28 14:20:03 -040036struct amdgpu_cgs_device {
37 struct cgs_device base;
38 struct amdgpu_device *adev;
39};
40
41#define CGS_FUNC_ADEV \
42 struct amdgpu_device *adev = \
43 ((struct amdgpu_cgs_device *)cgs_device)->adev
44
Rex Zhuba89a3e2017-09-25 20:45:52 +080045static void *amdgpu_cgs_register_pp_handle(struct cgs_device *cgs_device,
46 int (*call_back_func)(struct amd_pp_init *, void **))
47{
48 CGS_FUNC_ADEV;
49 struct amd_pp_init pp_init;
50 struct amd_powerplay *amd_pp;
51
52 if (call_back_func == NULL)
53 return NULL;
54
55 amd_pp = &(adev->powerplay);
56 pp_init.chip_family = adev->family;
57 pp_init.chip_id = adev->asic_type;
58 pp_init.pm_en = (amdgpu_dpm != 0 && !amdgpu_sriov_vf(adev)) ? true : false;
59 pp_init.feature_mask = amdgpu_pp_feature_mask;
60 pp_init.device = cgs_device;
61 if (call_back_func(&pp_init, &(amd_pp->pp_handle)))
62 return NULL;
63
64 return adev->powerplay.pp_handle;
65}
66
Dave Airlie110e6f22016-04-12 13:25:48 +100067static int amdgpu_cgs_alloc_gpu_mem(struct cgs_device *cgs_device,
Chunming Zhoud03846a2015-07-28 14:20:03 -040068 enum cgs_gpu_mem_type type,
69 uint64_t size, uint64_t align,
Chunming Zhoud03846a2015-07-28 14:20:03 -040070 cgs_handle_t *handle)
71{
Chunming Zhou57ff96c2015-04-24 17:38:20 +080072 CGS_FUNC_ADEV;
73 uint16_t flags = 0;
74 int ret = 0;
75 uint32_t domain = 0;
76 struct amdgpu_bo *obj;
Chunming Zhou57ff96c2015-04-24 17:38:20 +080077
78 /* fail if the alignment is not a power of 2 */
79 if (((align != 1) && (align & (align - 1)))
80 || size == 0 || align == 0)
81 return -EINVAL;
82
83
84 switch(type) {
85 case CGS_GPU_MEM_TYPE__VISIBLE_CONTIG_FB:
86 case CGS_GPU_MEM_TYPE__VISIBLE_FB:
Christian König03f48dd2016-08-15 17:00:22 +020087 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
88 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Chunming Zhou57ff96c2015-04-24 17:38:20 +080089 domain = AMDGPU_GEM_DOMAIN_VRAM;
Chunming Zhou57ff96c2015-04-24 17:38:20 +080090 break;
91 case CGS_GPU_MEM_TYPE__INVISIBLE_CONTIG_FB:
92 case CGS_GPU_MEM_TYPE__INVISIBLE_FB:
Christian König03f48dd2016-08-15 17:00:22 +020093 flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
94 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
Chunming Zhou57ff96c2015-04-24 17:38:20 +080095 domain = AMDGPU_GEM_DOMAIN_VRAM;
Chunming Zhou57ff96c2015-04-24 17:38:20 +080096 break;
97 case CGS_GPU_MEM_TYPE__GART_CACHEABLE:
98 domain = AMDGPU_GEM_DOMAIN_GTT;
Chunming Zhou57ff96c2015-04-24 17:38:20 +080099 break;
100 case CGS_GPU_MEM_TYPE__GART_WRITECOMBINE:
101 flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
102 domain = AMDGPU_GEM_DOMAIN_GTT;
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800103 break;
104 default:
105 return -EINVAL;
106 }
107
108
109 *handle = 0;
110
Christian König88531912017-09-11 17:10:26 +0200111 ret = amdgpu_bo_create(adev, size, align, true, domain, flags,
112 NULL, NULL, 0, &obj);
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800113 if (ret) {
114 DRM_ERROR("(%d) bo create failed\n", ret);
115 return ret;
116 }
117 *handle = (cgs_handle_t)obj;
118
119 return ret;
Chunming Zhoud03846a2015-07-28 14:20:03 -0400120}
121
Dave Airlie110e6f22016-04-12 13:25:48 +1000122static int amdgpu_cgs_free_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle)
Chunming Zhoud03846a2015-07-28 14:20:03 -0400123{
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800124 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
125
126 if (obj) {
Alex Xiecca7ecb2017-04-26 13:31:01 -0400127 int r = amdgpu_bo_reserve(obj, true);
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800128 if (likely(r == 0)) {
129 amdgpu_bo_kunmap(obj);
130 amdgpu_bo_unpin(obj);
131 amdgpu_bo_unreserve(obj);
132 }
133 amdgpu_bo_unref(&obj);
134
135 }
Chunming Zhoud03846a2015-07-28 14:20:03 -0400136 return 0;
137}
138
Dave Airlie110e6f22016-04-12 13:25:48 +1000139static int amdgpu_cgs_gmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle,
Chunming Zhoud03846a2015-07-28 14:20:03 -0400140 uint64_t *mcaddr)
141{
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800142 int r;
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800143 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
144
145 WARN_ON_ONCE(obj->placement.num_placement > 1);
146
Alex Xiecca7ecb2017-04-26 13:31:01 -0400147 r = amdgpu_bo_reserve(obj, true);
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800148 if (unlikely(r != 0))
149 return r;
Christian König88531912017-09-11 17:10:26 +0200150 r = amdgpu_bo_pin(obj, obj->preferred_domains, mcaddr);
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800151 amdgpu_bo_unreserve(obj);
152 return r;
Chunming Zhoud03846a2015-07-28 14:20:03 -0400153}
154
Dave Airlie110e6f22016-04-12 13:25:48 +1000155static int amdgpu_cgs_gunmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle)
Chunming Zhoud03846a2015-07-28 14:20:03 -0400156{
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800157 int r;
158 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
Alex Xiecca7ecb2017-04-26 13:31:01 -0400159 r = amdgpu_bo_reserve(obj, true);
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800160 if (unlikely(r != 0))
161 return r;
162 r = amdgpu_bo_unpin(obj);
163 amdgpu_bo_unreserve(obj);
164 return r;
Chunming Zhoud03846a2015-07-28 14:20:03 -0400165}
166
Dave Airlie110e6f22016-04-12 13:25:48 +1000167static int amdgpu_cgs_kmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle,
Chunming Zhoud03846a2015-07-28 14:20:03 -0400168 void **map)
169{
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800170 int r;
171 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
Alex Xiecca7ecb2017-04-26 13:31:01 -0400172 r = amdgpu_bo_reserve(obj, true);
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800173 if (unlikely(r != 0))
174 return r;
175 r = amdgpu_bo_kmap(obj, map);
176 amdgpu_bo_unreserve(obj);
177 return r;
Chunming Zhoud03846a2015-07-28 14:20:03 -0400178}
179
Dave Airlie110e6f22016-04-12 13:25:48 +1000180static int amdgpu_cgs_kunmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle)
Chunming Zhoud03846a2015-07-28 14:20:03 -0400181{
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800182 int r;
183 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
Alex Xiecca7ecb2017-04-26 13:31:01 -0400184 r = amdgpu_bo_reserve(obj, true);
Chunming Zhou57ff96c2015-04-24 17:38:20 +0800185 if (unlikely(r != 0))
186 return r;
187 amdgpu_bo_kunmap(obj);
188 amdgpu_bo_unreserve(obj);
189 return r;
Chunming Zhoud03846a2015-07-28 14:20:03 -0400190}
191
Dave Airlie110e6f22016-04-12 13:25:48 +1000192static uint32_t amdgpu_cgs_read_register(struct cgs_device *cgs_device, unsigned offset)
Chunming Zhoud03846a2015-07-28 14:20:03 -0400193{
Chunming Zhouaba684d2015-05-22 11:29:30 -0400194 CGS_FUNC_ADEV;
195 return RREG32(offset);
Chunming Zhoud03846a2015-07-28 14:20:03 -0400196}
197
Dave Airlie110e6f22016-04-12 13:25:48 +1000198static void amdgpu_cgs_write_register(struct cgs_device *cgs_device, unsigned offset,
Chunming Zhoud03846a2015-07-28 14:20:03 -0400199 uint32_t value)
200{
Chunming Zhouaba684d2015-05-22 11:29:30 -0400201 CGS_FUNC_ADEV;
202 WREG32(offset, value);
Chunming Zhoud03846a2015-07-28 14:20:03 -0400203}
204
Dave Airlie110e6f22016-04-12 13:25:48 +1000205static uint32_t amdgpu_cgs_read_ind_register(struct cgs_device *cgs_device,
Chunming Zhoud03846a2015-07-28 14:20:03 -0400206 enum cgs_ind_reg space,
207 unsigned index)
208{
Chunming Zhouaba684d2015-05-22 11:29:30 -0400209 CGS_FUNC_ADEV;
210 switch (space) {
211 case CGS_IND_REG__MMIO:
212 return RREG32_IDX(index);
213 case CGS_IND_REG__PCIE:
214 return RREG32_PCIE(index);
215 case CGS_IND_REG__SMC:
216 return RREG32_SMC(index);
217 case CGS_IND_REG__UVD_CTX:
218 return RREG32_UVD_CTX(index);
219 case CGS_IND_REG__DIDT:
220 return RREG32_DIDT(index);
Rex Zhuccdbb202016-06-08 12:47:41 +0800221 case CGS_IND_REG_GC_CAC:
222 return RREG32_GC_CAC(index);
Evan Quanc62a59d2017-07-04 09:24:34 +0800223 case CGS_IND_REG_SE_CAC:
224 return RREG32_SE_CAC(index);
Chunming Zhouaba684d2015-05-22 11:29:30 -0400225 case CGS_IND_REG__AUDIO_ENDPT:
226 DRM_ERROR("audio endpt register access not implemented.\n");
227 return 0;
228 }
229 WARN(1, "Invalid indirect register space");
Chunming Zhoud03846a2015-07-28 14:20:03 -0400230 return 0;
231}
232
Dave Airlie110e6f22016-04-12 13:25:48 +1000233static void amdgpu_cgs_write_ind_register(struct cgs_device *cgs_device,
Chunming Zhoud03846a2015-07-28 14:20:03 -0400234 enum cgs_ind_reg space,
235 unsigned index, uint32_t value)
236{
Chunming Zhouaba684d2015-05-22 11:29:30 -0400237 CGS_FUNC_ADEV;
238 switch (space) {
239 case CGS_IND_REG__MMIO:
240 return WREG32_IDX(index, value);
241 case CGS_IND_REG__PCIE:
242 return WREG32_PCIE(index, value);
243 case CGS_IND_REG__SMC:
244 return WREG32_SMC(index, value);
245 case CGS_IND_REG__UVD_CTX:
246 return WREG32_UVD_CTX(index, value);
247 case CGS_IND_REG__DIDT:
248 return WREG32_DIDT(index, value);
Rex Zhuccdbb202016-06-08 12:47:41 +0800249 case CGS_IND_REG_GC_CAC:
250 return WREG32_GC_CAC(index, value);
Evan Quanc62a59d2017-07-04 09:24:34 +0800251 case CGS_IND_REG_SE_CAC:
252 return WREG32_SE_CAC(index, value);
Chunming Zhouaba684d2015-05-22 11:29:30 -0400253 case CGS_IND_REG__AUDIO_ENDPT:
254 DRM_ERROR("audio endpt register access not implemented.\n");
255 return;
256 }
257 WARN(1, "Invalid indirect register space");
Chunming Zhoud03846a2015-07-28 14:20:03 -0400258}
259
Dave Airlie110e6f22016-04-12 13:25:48 +1000260static int amdgpu_cgs_get_pci_resource(struct cgs_device *cgs_device,
Alex Deucherba228ac2015-12-23 11:25:43 -0500261 enum cgs_resource_type resource_type,
262 uint64_t size,
263 uint64_t offset,
264 uint64_t *resource_base)
265{
266 CGS_FUNC_ADEV;
267
268 if (resource_base == NULL)
269 return -EINVAL;
270
271 switch (resource_type) {
272 case CGS_RESOURCE_TYPE_MMIO:
273 if (adev->rmmio_size == 0)
274 return -ENOENT;
275 if ((offset + size) > adev->rmmio_size)
276 return -EINVAL;
277 *resource_base = adev->rmmio_base;
278 return 0;
279 case CGS_RESOURCE_TYPE_DOORBELL:
280 if (adev->doorbell.size == 0)
281 return -ENOENT;
282 if ((offset + size) > adev->doorbell.size)
283 return -EINVAL;
284 *resource_base = adev->doorbell.base;
285 return 0;
286 case CGS_RESOURCE_TYPE_FB:
287 case CGS_RESOURCE_TYPE_IO:
288 case CGS_RESOURCE_TYPE_ROM:
289 default:
290 return -EINVAL;
291 }
292}
293
Dave Airlie110e6f22016-04-12 13:25:48 +1000294static const void *amdgpu_cgs_atom_get_data_table(struct cgs_device *cgs_device,
Chunming Zhoud03846a2015-07-28 14:20:03 -0400295 unsigned table, uint16_t *size,
296 uint8_t *frev, uint8_t *crev)
297{
Chunming Zhou25da4422015-05-22 12:14:04 -0400298 CGS_FUNC_ADEV;
299 uint16_t data_start;
300
301 if (amdgpu_atom_parse_data_header(
302 adev->mode_info.atom_context, table, size,
303 frev, crev, &data_start))
304 return (uint8_t*)adev->mode_info.atom_context->bios +
305 data_start;
306
Chunming Zhoud03846a2015-07-28 14:20:03 -0400307 return NULL;
308}
309
Dave Airlie110e6f22016-04-12 13:25:48 +1000310static int amdgpu_cgs_atom_get_cmd_table_revs(struct cgs_device *cgs_device, unsigned table,
Chunming Zhoud03846a2015-07-28 14:20:03 -0400311 uint8_t *frev, uint8_t *crev)
312{
Chunming Zhou25da4422015-05-22 12:14:04 -0400313 CGS_FUNC_ADEV;
314
315 if (amdgpu_atom_parse_cmd_header(
316 adev->mode_info.atom_context, table,
317 frev, crev))
318 return 0;
319
320 return -EINVAL;
Chunming Zhoud03846a2015-07-28 14:20:03 -0400321}
322
Dave Airlie110e6f22016-04-12 13:25:48 +1000323static int amdgpu_cgs_atom_exec_cmd_table(struct cgs_device *cgs_device, unsigned table,
Chunming Zhoud03846a2015-07-28 14:20:03 -0400324 void *args)
325{
Chunming Zhou25da4422015-05-22 12:14:04 -0400326 CGS_FUNC_ADEV;
Chunming Zhoud03846a2015-07-28 14:20:03 -0400327
Chunming Zhou25da4422015-05-22 12:14:04 -0400328 return amdgpu_atom_execute_table(
329 adev->mode_info.atom_context, table, args);
330}
Chunming Zhoud03846a2015-07-28 14:20:03 -0400331
Alex Deucher0cf3be22015-07-28 14:24:53 -0400332struct cgs_irq_params {
333 unsigned src_id;
334 cgs_irq_source_set_func_t set;
335 cgs_irq_handler_func_t handler;
336 void *private_data;
337};
338
339static int cgs_set_irq_state(struct amdgpu_device *adev,
340 struct amdgpu_irq_src *src,
341 unsigned type,
342 enum amdgpu_interrupt_state state)
343{
344 struct cgs_irq_params *irq_params =
345 (struct cgs_irq_params *)src->data;
346 if (!irq_params)
347 return -EINVAL;
348 if (!irq_params->set)
349 return -EINVAL;
350 return irq_params->set(irq_params->private_data,
351 irq_params->src_id,
352 type,
353 (int)state);
354}
355
356static int cgs_process_irq(struct amdgpu_device *adev,
357 struct amdgpu_irq_src *source,
358 struct amdgpu_iv_entry *entry)
359{
360 struct cgs_irq_params *irq_params =
361 (struct cgs_irq_params *)source->data;
362 if (!irq_params)
363 return -EINVAL;
364 if (!irq_params->handler)
365 return -EINVAL;
366 return irq_params->handler(irq_params->private_data,
367 irq_params->src_id,
368 entry->iv_entry);
369}
370
371static const struct amdgpu_irq_src_funcs cgs_irq_funcs = {
372 .set = cgs_set_irq_state,
373 .process = cgs_process_irq,
374};
375
Alex Deucherd766e6a2016-03-29 18:28:50 -0400376static int amdgpu_cgs_add_irq_source(void *cgs_device,
377 unsigned client_id,
378 unsigned src_id,
Chunming Zhoud03846a2015-07-28 14:20:03 -0400379 unsigned num_types,
380 cgs_irq_source_set_func_t set,
381 cgs_irq_handler_func_t handler,
382 void *private_data)
383{
Alex Deucher0cf3be22015-07-28 14:24:53 -0400384 CGS_FUNC_ADEV;
385 int ret = 0;
386 struct cgs_irq_params *irq_params;
387 struct amdgpu_irq_src *source =
388 kzalloc(sizeof(struct amdgpu_irq_src), GFP_KERNEL);
389 if (!source)
390 return -ENOMEM;
391 irq_params =
392 kzalloc(sizeof(struct cgs_irq_params), GFP_KERNEL);
393 if (!irq_params) {
394 kfree(source);
395 return -ENOMEM;
396 }
397 source->num_types = num_types;
398 source->funcs = &cgs_irq_funcs;
399 irq_params->src_id = src_id;
400 irq_params->set = set;
401 irq_params->handler = handler;
402 irq_params->private_data = private_data;
403 source->data = (void *)irq_params;
Alex Deucherd766e6a2016-03-29 18:28:50 -0400404 ret = amdgpu_irq_add_id(adev, client_id, src_id, source);
Alex Deucher0cf3be22015-07-28 14:24:53 -0400405 if (ret) {
406 kfree(irq_params);
407 kfree(source);
408 }
409
410 return ret;
Chunming Zhoud03846a2015-07-28 14:20:03 -0400411}
412
Alex Deucherd766e6a2016-03-29 18:28:50 -0400413static int amdgpu_cgs_irq_get(void *cgs_device, unsigned client_id,
414 unsigned src_id, unsigned type)
Chunming Zhoud03846a2015-07-28 14:20:03 -0400415{
Alex Deucher0cf3be22015-07-28 14:24:53 -0400416 CGS_FUNC_ADEV;
Alex Deucherd766e6a2016-03-29 18:28:50 -0400417
418 if (!adev->irq.client[client_id].sources)
419 return -EINVAL;
420
421 return amdgpu_irq_get(adev, adev->irq.client[client_id].sources[src_id], type);
Chunming Zhoud03846a2015-07-28 14:20:03 -0400422}
423
Alex Deucherd766e6a2016-03-29 18:28:50 -0400424static int amdgpu_cgs_irq_put(void *cgs_device, unsigned client_id,
425 unsigned src_id, unsigned type)
Chunming Zhoud03846a2015-07-28 14:20:03 -0400426{
Alex Deucher0cf3be22015-07-28 14:24:53 -0400427 CGS_FUNC_ADEV;
Alex Deucherd766e6a2016-03-29 18:28:50 -0400428
429 if (!adev->irq.client[client_id].sources)
430 return -EINVAL;
431
432 return amdgpu_irq_put(adev, adev->irq.client[client_id].sources[src_id], type);
Chunming Zhoud03846a2015-07-28 14:20:03 -0400433}
434
Baoyou Xie761c2e82016-09-03 13:57:14 +0800435static int amdgpu_cgs_set_clockgating_state(struct cgs_device *cgs_device,
rezhu404b2fa2015-08-07 13:37:56 +0800436 enum amd_ip_block_type block_type,
437 enum amd_clockgating_state state)
438{
439 CGS_FUNC_ADEV;
440 int i, r = -1;
441
442 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400443 if (!adev->ip_blocks[i].status.valid)
rezhu404b2fa2015-08-07 13:37:56 +0800444 continue;
445
Alex Deuchera1255102016-10-13 17:41:13 -0400446 if (adev->ip_blocks[i].version->type == block_type) {
447 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
rezhu404b2fa2015-08-07 13:37:56 +0800448 (void *)adev,
449 state);
450 break;
451 }
452 }
453 return r;
454}
455
Baoyou Xie761c2e82016-09-03 13:57:14 +0800456static int amdgpu_cgs_set_powergating_state(struct cgs_device *cgs_device,
rezhu404b2fa2015-08-07 13:37:56 +0800457 enum amd_ip_block_type block_type,
458 enum amd_powergating_state state)
459{
460 CGS_FUNC_ADEV;
461 int i, r = -1;
462
463 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400464 if (!adev->ip_blocks[i].status.valid)
rezhu404b2fa2015-08-07 13:37:56 +0800465 continue;
466
Alex Deuchera1255102016-10-13 17:41:13 -0400467 if (adev->ip_blocks[i].version->type == block_type) {
468 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
rezhu404b2fa2015-08-07 13:37:56 +0800469 (void *)adev,
470 state);
471 break;
472 }
473 }
474 return r;
475}
476
477
Dave Airlie110e6f22016-04-12 13:25:48 +1000478static uint32_t fw_type_convert(struct cgs_device *cgs_device, uint32_t fw_type)
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800479{
480 CGS_FUNC_ADEV;
481 enum AMDGPU_UCODE_ID result = AMDGPU_UCODE_ID_MAXIMUM;
482
483 switch (fw_type) {
484 case CGS_UCODE_ID_SDMA0:
485 result = AMDGPU_UCODE_ID_SDMA0;
486 break;
487 case CGS_UCODE_ID_SDMA1:
488 result = AMDGPU_UCODE_ID_SDMA1;
489 break;
490 case CGS_UCODE_ID_CP_CE:
491 result = AMDGPU_UCODE_ID_CP_CE;
492 break;
493 case CGS_UCODE_ID_CP_PFP:
494 result = AMDGPU_UCODE_ID_CP_PFP;
495 break;
496 case CGS_UCODE_ID_CP_ME:
497 result = AMDGPU_UCODE_ID_CP_ME;
498 break;
499 case CGS_UCODE_ID_CP_MEC:
500 case CGS_UCODE_ID_CP_MEC_JT1:
501 result = AMDGPU_UCODE_ID_CP_MEC1;
502 break;
503 case CGS_UCODE_ID_CP_MEC_JT2:
Monk Liu4c2b2452016-09-27 16:39:58 +0800504 /* for VI. JT2 should be the same as JT1, because:
505 1, MEC2 and MEC1 use exactly same FW.
506 2, JT2 is not pached but JT1 is.
507 */
508 if (adev->asic_type >= CHIP_TOPAZ)
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800509 result = AMDGPU_UCODE_ID_CP_MEC1;
Monk Liu4c2b2452016-09-27 16:39:58 +0800510 else
511 result = AMDGPU_UCODE_ID_CP_MEC2;
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800512 break;
513 case CGS_UCODE_ID_RLC_G:
514 result = AMDGPU_UCODE_ID_RLC_G;
515 break;
Monk Liubed57122016-09-26 16:35:03 +0800516 case CGS_UCODE_ID_STORAGE:
517 result = AMDGPU_UCODE_ID_STORAGE;
518 break;
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800519 default:
520 DRM_ERROR("Firmware type not supported\n");
521 }
522 return result;
523}
524
Monk Liua3927462016-05-31 13:44:30 +0800525static int amdgpu_cgs_rel_firmware(struct cgs_device *cgs_device, enum cgs_ucode_id type)
526{
527 CGS_FUNC_ADEV;
528 if ((CGS_UCODE_ID_SMU == type) || (CGS_UCODE_ID_SMU_SK == type)) {
529 release_firmware(adev->pm.fw);
Huang Rui5c1104b2016-12-19 15:15:35 +0800530 adev->pm.fw = NULL;
Monk Liua3927462016-05-31 13:44:30 +0800531 return 0;
532 }
533 /* cannot release other firmware because they are not created by cgs */
534 return -EINVAL;
535}
536
Frank Minfc76cbf2016-04-27 18:53:29 +0800537static uint16_t amdgpu_get_firmware_version(struct cgs_device *cgs_device,
538 enum cgs_ucode_id type)
539{
540 CGS_FUNC_ADEV;
Xiangliang Yu188a3012016-11-24 16:28:46 +0800541 uint16_t fw_version = 0;
Frank Minfc76cbf2016-04-27 18:53:29 +0800542
543 switch (type) {
544 case CGS_UCODE_ID_SDMA0:
545 fw_version = adev->sdma.instance[0].fw_version;
546 break;
547 case CGS_UCODE_ID_SDMA1:
548 fw_version = adev->sdma.instance[1].fw_version;
549 break;
550 case CGS_UCODE_ID_CP_CE:
551 fw_version = adev->gfx.ce_fw_version;
552 break;
553 case CGS_UCODE_ID_CP_PFP:
554 fw_version = adev->gfx.pfp_fw_version;
555 break;
556 case CGS_UCODE_ID_CP_ME:
557 fw_version = adev->gfx.me_fw_version;
558 break;
559 case CGS_UCODE_ID_CP_MEC:
560 fw_version = adev->gfx.mec_fw_version;
561 break;
562 case CGS_UCODE_ID_CP_MEC_JT1:
563 fw_version = adev->gfx.mec_fw_version;
564 break;
565 case CGS_UCODE_ID_CP_MEC_JT2:
566 fw_version = adev->gfx.mec_fw_version;
567 break;
568 case CGS_UCODE_ID_RLC_G:
569 fw_version = adev->gfx.rlc_fw_version;
570 break;
Xiangliang Yu188a3012016-11-24 16:28:46 +0800571 case CGS_UCODE_ID_STORAGE:
572 break;
Frank Minfc76cbf2016-04-27 18:53:29 +0800573 default:
574 DRM_ERROR("firmware type %d do not have version\n", type);
Xiangliang Yu188a3012016-11-24 16:28:46 +0800575 break;
Frank Minfc76cbf2016-04-27 18:53:29 +0800576 }
577 return fw_version;
578}
579
Rex Zhue8a95b22016-12-21 20:30:58 +0800580static int amdgpu_cgs_enter_safe_mode(struct cgs_device *cgs_device,
581 bool en)
582{
583 CGS_FUNC_ADEV;
584
585 if (adev->gfx.rlc.funcs->enter_safe_mode == NULL ||
586 adev->gfx.rlc.funcs->exit_safe_mode == NULL)
587 return 0;
588
589 if (en)
590 adev->gfx.rlc.funcs->enter_safe_mode(adev);
591 else
592 adev->gfx.rlc.funcs->exit_safe_mode(adev);
593
594 return 0;
595}
596
Evan Quan209ee272017-07-04 15:37:09 +0800597static void amdgpu_cgs_lock_grbm_idx(struct cgs_device *cgs_device,
598 bool lock)
599{
600 CGS_FUNC_ADEV;
601
602 if (lock)
603 mutex_lock(&adev->grbm_idx_mutex);
604 else
605 mutex_unlock(&adev->grbm_idx_mutex);
606}
607
Dave Airlie110e6f22016-04-12 13:25:48 +1000608static int amdgpu_cgs_get_firmware_info(struct cgs_device *cgs_device,
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800609 enum cgs_ucode_id type,
610 struct cgs_firmware_info *info)
611{
612 CGS_FUNC_ADEV;
613
yanyang1735f0022016-02-05 17:39:37 +0800614 if ((CGS_UCODE_ID_SMU != type) && (CGS_UCODE_ID_SMU_SK != type)) {
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800615 uint64_t gpu_addr;
616 uint32_t data_size;
617 const struct gfx_firmware_header_v1_0 *header;
618 enum AMDGPU_UCODE_ID id;
619 struct amdgpu_firmware_info *ucode;
620
621 id = fw_type_convert(cgs_device, type);
622 ucode = &adev->firmware.ucode[id];
623 if (ucode->fw == NULL)
624 return -EINVAL;
625
626 gpu_addr = ucode->mc_addr;
627 header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
628 data_size = le32_to_cpu(header->header.ucode_size_bytes);
629
630 if ((type == CGS_UCODE_ID_CP_MEC_JT1) ||
631 (type == CGS_UCODE_ID_CP_MEC_JT2)) {
Monk Liu4c2b2452016-09-27 16:39:58 +0800632 gpu_addr += ALIGN(le32_to_cpu(header->header.ucode_size_bytes), PAGE_SIZE);
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800633 data_size = le32_to_cpu(header->jt_size) << 2;
634 }
Monk Liu4c2b2452016-09-27 16:39:58 +0800635
636 info->kptr = ucode->kaddr;
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800637 info->image_size = data_size;
Monk Liu4c2b2452016-09-27 16:39:58 +0800638 info->mc_addr = gpu_addr;
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800639 info->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
Monk Liu4c2b2452016-09-27 16:39:58 +0800640
641 if (CGS_UCODE_ID_CP_MEC == type)
Evan Quane68760b2017-08-10 15:17:56 +0800642 info->image_size = le32_to_cpu(header->jt_offset) << 2;
Monk Liu4c2b2452016-09-27 16:39:58 +0800643
Frank Minfc76cbf2016-04-27 18:53:29 +0800644 info->fw_version = amdgpu_get_firmware_version(cgs_device, type);
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800645 info->feature_version = (uint16_t)le32_to_cpu(header->ucode_feature_version);
646 } else {
647 char fw_name[30] = {0};
648 int err = 0;
649 uint32_t ucode_size;
650 uint32_t ucode_start_address;
651 const uint8_t *src;
652 const struct smc_firmware_header_v1_0 *hdr;
Huang Ruid1de1ed2017-02-16 11:53:38 +0800653 const struct common_firmware_header *header;
654 struct amdgpu_firmware_info *ucode = NULL;
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800655
Mykola Lysenko0b455412016-03-30 05:50:11 -0400656 if (!adev->pm.fw) {
657 switch (adev->asic_type) {
Rex Zhu6df98552017-09-08 14:05:51 +0800658 case CHIP_TAHITI:
659 strcpy(fw_name, "radeon/tahiti_smc.bin");
660 break;
661 case CHIP_PITCAIRN:
662 if ((adev->pdev->revision == 0x81) &&
663 ((adev->pdev->device == 0x6810) ||
664 (adev->pdev->device == 0x6811))) {
665 info->is_kicker = true;
666 strcpy(fw_name, "radeon/pitcairn_k_smc.bin");
667 } else {
668 strcpy(fw_name, "radeon/pitcairn_smc.bin");
669 }
670 break;
671 case CHIP_VERDE:
672 if (((adev->pdev->device == 0x6820) &&
673 ((adev->pdev->revision == 0x81) ||
674 (adev->pdev->revision == 0x83))) ||
675 ((adev->pdev->device == 0x6821) &&
676 ((adev->pdev->revision == 0x83) ||
677 (adev->pdev->revision == 0x87))) ||
678 ((adev->pdev->revision == 0x87) &&
679 ((adev->pdev->device == 0x6823) ||
680 (adev->pdev->device == 0x682b)))) {
681 info->is_kicker = true;
682 strcpy(fw_name, "radeon/verde_k_smc.bin");
683 } else {
684 strcpy(fw_name, "radeon/verde_smc.bin");
685 }
686 break;
687 case CHIP_OLAND:
688 if (((adev->pdev->revision == 0x81) &&
689 ((adev->pdev->device == 0x6600) ||
690 (adev->pdev->device == 0x6604) ||
691 (adev->pdev->device == 0x6605) ||
692 (adev->pdev->device == 0x6610))) ||
693 ((adev->pdev->revision == 0x83) &&
694 (adev->pdev->device == 0x6610))) {
695 info->is_kicker = true;
696 strcpy(fw_name, "radeon/oland_k_smc.bin");
697 } else {
698 strcpy(fw_name, "radeon/oland_smc.bin");
699 }
700 break;
701 case CHIP_HAINAN:
702 if (((adev->pdev->revision == 0x81) &&
703 (adev->pdev->device == 0x6660)) ||
704 ((adev->pdev->revision == 0x83) &&
705 ((adev->pdev->device == 0x6660) ||
706 (adev->pdev->device == 0x6663) ||
707 (adev->pdev->device == 0x6665) ||
708 (adev->pdev->device == 0x6667)))) {
709 info->is_kicker = true;
710 strcpy(fw_name, "radeon/hainan_k_smc.bin");
711 } else if ((adev->pdev->revision == 0xc3) &&
712 (adev->pdev->device == 0x6665)) {
713 info->is_kicker = true;
714 strcpy(fw_name, "radeon/banks_k_2_smc.bin");
715 } else {
716 strcpy(fw_name, "radeon/hainan_smc.bin");
717 }
718 break;
719 case CHIP_BONAIRE:
720 if ((adev->pdev->revision == 0x80) ||
721 (adev->pdev->revision == 0x81) ||
722 (adev->pdev->device == 0x665f)) {
723 info->is_kicker = true;
724 strcpy(fw_name, "radeon/bonaire_k_smc.bin");
725 } else {
726 strcpy(fw_name, "radeon/bonaire_smc.bin");
727 }
728 break;
729 case CHIP_HAWAII:
730 if (adev->pdev->revision == 0x80) {
731 info->is_kicker = true;
732 strcpy(fw_name, "radeon/hawaii_k_smc.bin");
733 } else {
734 strcpy(fw_name, "radeon/hawaii_smc.bin");
735 }
736 break;
Huang Rui340efe22016-06-19 23:55:14 +0800737 case CHIP_TOPAZ:
Alex Deucher3b496622016-10-27 18:33:00 -0400738 if (((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x81)) ||
739 ((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x83)) ||
Huang Rui5d7213b2017-02-10 16:42:19 +0800740 ((adev->pdev->device == 0x6907) && (adev->pdev->revision == 0x87))) {
741 info->is_kicker = true;
Alex Deucher3b496622016-10-27 18:33:00 -0400742 strcpy(fw_name, "amdgpu/topaz_k_smc.bin");
Huang Rui5d7213b2017-02-10 16:42:19 +0800743 } else
Alex Deucher3b496622016-10-27 18:33:00 -0400744 strcpy(fw_name, "amdgpu/topaz_smc.bin");
Huang Rui340efe22016-06-19 23:55:14 +0800745 break;
Mykola Lysenko0b455412016-03-30 05:50:11 -0400746 case CHIP_TONGA:
Alex Deucher646cccb2016-10-26 16:41:39 -0400747 if (((adev->pdev->device == 0x6939) && (adev->pdev->revision == 0xf1)) ||
Huang Rui5d7213b2017-02-10 16:42:19 +0800748 ((adev->pdev->device == 0x6938) && (adev->pdev->revision == 0xf1))) {
749 info->is_kicker = true;
Alex Deucher646cccb2016-10-26 16:41:39 -0400750 strcpy(fw_name, "amdgpu/tonga_k_smc.bin");
Huang Rui5d7213b2017-02-10 16:42:19 +0800751 } else
Alex Deucher646cccb2016-10-26 16:41:39 -0400752 strcpy(fw_name, "amdgpu/tonga_smc.bin");
Mykola Lysenko0b455412016-03-30 05:50:11 -0400753 break;
754 case CHIP_FIJI:
755 strcpy(fw_name, "amdgpu/fiji_smc.bin");
756 break;
757 case CHIP_POLARIS11:
Alex Deuchera52d1202017-02-08 22:35:51 -0500758 if (type == CGS_UCODE_ID_SMU) {
759 if (((adev->pdev->device == 0x67ef) &&
760 ((adev->pdev->revision == 0xe0) ||
761 (adev->pdev->revision == 0xe2) ||
762 (adev->pdev->revision == 0xe5))) ||
763 ((adev->pdev->device == 0x67ff) &&
764 ((adev->pdev->revision == 0xcf) ||
765 (adev->pdev->revision == 0xef) ||
Huang Rui5d7213b2017-02-10 16:42:19 +0800766 (adev->pdev->revision == 0xff)))) {
767 info->is_kicker = true;
Alex Deuchera52d1202017-02-08 22:35:51 -0500768 strcpy(fw_name, "amdgpu/polaris11_k_smc.bin");
Huang Rui5d7213b2017-02-10 16:42:19 +0800769 } else
Alex Deuchera52d1202017-02-08 22:35:51 -0500770 strcpy(fw_name, "amdgpu/polaris11_smc.bin");
771 } else if (type == CGS_UCODE_ID_SMU_SK) {
Mykola Lysenko0b455412016-03-30 05:50:11 -0400772 strcpy(fw_name, "amdgpu/polaris11_smc_sk.bin");
Alex Deuchera52d1202017-02-08 22:35:51 -0500773 }
Mykola Lysenko0b455412016-03-30 05:50:11 -0400774 break;
775 case CHIP_POLARIS10:
Alex Deuchera52d1202017-02-08 22:35:51 -0500776 if (type == CGS_UCODE_ID_SMU) {
777 if ((adev->pdev->device == 0x67df) &&
778 ((adev->pdev->revision == 0xe0) ||
779 (adev->pdev->revision == 0xe3) ||
780 (adev->pdev->revision == 0xe4) ||
781 (adev->pdev->revision == 0xe5) ||
782 (adev->pdev->revision == 0xe7) ||
Huang Rui5d7213b2017-02-10 16:42:19 +0800783 (adev->pdev->revision == 0xef))) {
784 info->is_kicker = true;
Alex Deuchera52d1202017-02-08 22:35:51 -0500785 strcpy(fw_name, "amdgpu/polaris10_k_smc.bin");
Huang Rui5d7213b2017-02-10 16:42:19 +0800786 } else
Alex Deuchera52d1202017-02-08 22:35:51 -0500787 strcpy(fw_name, "amdgpu/polaris10_smc.bin");
788 } else if (type == CGS_UCODE_ID_SMU_SK) {
Mykola Lysenko0b455412016-03-30 05:50:11 -0400789 strcpy(fw_name, "amdgpu/polaris10_smc_sk.bin");
Alex Deuchera52d1202017-02-08 22:35:51 -0500790 }
Mykola Lysenko0b455412016-03-30 05:50:11 -0400791 break;
Junwei Zhangc4642a42016-12-14 15:32:28 -0500792 case CHIP_POLARIS12:
793 strcpy(fw_name, "amdgpu/polaris12_smc.bin");
794 break;
Ken Wang220ab9b2017-03-06 14:49:53 -0500795 case CHIP_VEGA10:
Evan Quan747f6c92017-06-23 15:08:15 +0800796 if ((adev->pdev->device == 0x687f) &&
797 ((adev->pdev->revision == 0xc0) ||
798 (adev->pdev->revision == 0xc1) ||
799 (adev->pdev->revision == 0xc3)))
800 strcpy(fw_name, "amdgpu/vega10_acg_smc.bin");
801 else
802 strcpy(fw_name, "amdgpu/vega10_smc.bin");
Ken Wang220ab9b2017-03-06 14:49:53 -0500803 break;
Mykola Lysenko0b455412016-03-30 05:50:11 -0400804 default:
805 DRM_ERROR("SMC firmware not supported\n");
806 return -EINVAL;
807 }
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800808
Mykola Lysenko0b455412016-03-30 05:50:11 -0400809 err = request_firmware(&adev->pm.fw, fw_name, adev->dev);
810 if (err) {
811 DRM_ERROR("Failed to request firmware\n");
812 return err;
813 }
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800814
Mykola Lysenko0b455412016-03-30 05:50:11 -0400815 err = amdgpu_ucode_validate(adev->pm.fw);
816 if (err) {
817 DRM_ERROR("Failed to load firmware \"%s\"", fw_name);
818 release_firmware(adev->pm.fw);
819 adev->pm.fw = NULL;
820 return err;
821 }
Huang Ruid1de1ed2017-02-16 11:53:38 +0800822
823 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
824 ucode = &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
825 ucode->ucode_id = AMDGPU_UCODE_ID_SMC;
826 ucode->fw = adev->pm.fw;
827 header = (const struct common_firmware_header *)ucode->fw->data;
828 adev->firmware.fw_size +=
829 ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
830 }
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800831 }
832
833 hdr = (const struct smc_firmware_header_v1_0 *) adev->pm.fw->data;
yanyang1c66875b2016-05-30 15:30:54 +0800834 amdgpu_ucode_print_smc_hdr(&hdr->header);
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800835 adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
836 ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
837 ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
838 src = (const uint8_t *)(adev->pm.fw->data +
839 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
840
841 info->version = adev->pm.fw_version;
842 info->image_size = ucode_size;
Huang Rui340efe22016-06-19 23:55:14 +0800843 info->ucode_start_address = ucode_start_address;
Jammy Zhoubf3911b02015-05-13 18:58:05 +0800844 info->kptr = (void *)src;
845 }
846 return 0;
847}
848
Frank Minac00bbf2016-04-27 20:04:58 +0800849static int amdgpu_cgs_is_virtualization_enabled(void *cgs_device)
850{
851 CGS_FUNC_ADEV;
852 return amdgpu_sriov_vf(adev);
853}
854
Dave Airlie110e6f22016-04-12 13:25:48 +1000855static int amdgpu_cgs_query_system_info(struct cgs_device *cgs_device,
Huang Rui09fc7ef2016-07-12 13:54:05 +0800856 struct cgs_system_info *sys_info)
Rex Zhu5e618692015-09-23 20:11:54 +0800857{
858 CGS_FUNC_ADEV;
859
860 if (NULL == sys_info)
861 return -ENODEV;
862
863 if (sizeof(struct cgs_system_info) != sys_info->size)
864 return -ENODEV;
865
866 switch (sys_info->info_id) {
867 case CGS_SYSTEM_INFO_ADAPTER_BDF_ID:
868 sys_info->value = adev->pdev->devfn | (adev->pdev->bus->number << 8);
869 break;
Alex Deuchercfd316d2015-11-11 20:35:32 -0500870 case CGS_SYSTEM_INFO_PCIE_GEN_INFO:
871 sys_info->value = adev->pm.pcie_gen_mask;
872 break;
873 case CGS_SYSTEM_INFO_PCIE_MLW:
874 sys_info->value = adev->pm.pcie_mlw_mask;
875 break;
Huang Rui09fc7ef2016-07-12 13:54:05 +0800876 case CGS_SYSTEM_INFO_PCIE_DEV:
877 sys_info->value = adev->pdev->device;
878 break;
879 case CGS_SYSTEM_INFO_PCIE_REV:
880 sys_info->value = adev->pdev->revision;
881 break;
Alex Deucher08d33402016-02-05 10:34:28 -0500882 case CGS_SYSTEM_INFO_CG_FLAGS:
883 sys_info->value = adev->cg_flags;
884 break;
885 case CGS_SYSTEM_INFO_PG_FLAGS:
886 sys_info->value = adev->pg_flags;
887 break;
Eric Huangbacec892016-03-17 18:29:08 -0400888 case CGS_SYSTEM_INFO_GFX_CU_INFO:
Alex Deucher7dae69a2016-05-03 16:25:53 -0400889 sys_info->value = adev->gfx.cu_info.number;
Eric Huangbacec892016-03-17 18:29:08 -0400890 break;
Rex Zhud826c982016-06-07 20:15:24 +0800891 case CGS_SYSTEM_INFO_GFX_SE_INFO:
892 sys_info->value = adev->gfx.config.max_shader_engines;
893 break;
Rex Zhu2fef37c2016-08-22 20:48:13 +0800894 case CGS_SYSTEM_INFO_PCIE_SUB_SYS_ID:
895 sys_info->value = adev->pdev->subsystem_device;
896 break;
897 case CGS_SYSTEM_INFO_PCIE_SUB_SYS_VENDOR_ID:
898 sys_info->value = adev->pdev->subsystem_vendor;
899 break;
Eric Huang4d1f9fb2017-09-15 16:33:38 -0400900 case CGS_SYSTEM_INFO_PCIE_BUS_DEVFN:
901 sys_info->value = adev->pdev->devfn;
902 break;
Rex Zhu5e618692015-09-23 20:11:54 +0800903 default:
904 return -ENODEV;
905 }
906
907 return 0;
908}
909
Dave Airlie110e6f22016-04-12 13:25:48 +1000910static int amdgpu_cgs_get_active_displays_info(struct cgs_device *cgs_device,
Rex Zhu47bf18b2015-09-17 16:34:14 +0800911 struct cgs_display_info *info)
912{
913 CGS_FUNC_ADEV;
914 struct amdgpu_crtc *amdgpu_crtc;
915 struct drm_device *ddev = adev->ddev;
916 struct drm_crtc *crtc;
917 uint32_t line_time_us, vblank_lines;
Rex Zhuf9e9c082016-03-29 13:21:59 +0800918 struct cgs_mode_info *mode_info;
Rex Zhu47bf18b2015-09-17 16:34:14 +0800919
920 if (info == NULL)
921 return -EINVAL;
922
Rex Zhuf9e9c082016-03-29 13:21:59 +0800923 mode_info = info->mode_info;
Alex Deucher73cc9072017-06-30 09:58:34 -0400924 if (mode_info) {
Alex Deucherbeb37772017-06-29 16:08:49 -0400925 /* if the displays are off, vblank time is max */
926 mode_info->vblank_time_us = 0xffffffff;
Alex Deucher73cc9072017-06-30 09:58:34 -0400927 /* always set the reference clock */
928 mode_info->ref_clock = adev->clock.spll.reference_freq;
929 }
Rex Zhuf9e9c082016-03-29 13:21:59 +0800930
Rex Zhu47bf18b2015-09-17 16:34:14 +0800931 if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
932 list_for_each_entry(crtc,
933 &ddev->mode_config.crtc_list, head) {
934 amdgpu_crtc = to_amdgpu_crtc(crtc);
935 if (crtc->enabled) {
936 info->active_display_mask |= (1 << amdgpu_crtc->crtc_id);
937 info->display_count++;
938 }
Rex Zhuf9e9c082016-03-29 13:21:59 +0800939 if (mode_info != NULL &&
Rex Zhu47bf18b2015-09-17 16:34:14 +0800940 crtc->enabled && amdgpu_crtc->enabled &&
941 amdgpu_crtc->hw_mode.clock) {
942 line_time_us = (amdgpu_crtc->hw_mode.crtc_htotal * 1000) /
943 amdgpu_crtc->hw_mode.clock;
944 vblank_lines = amdgpu_crtc->hw_mode.crtc_vblank_end -
945 amdgpu_crtc->hw_mode.crtc_vdisplay +
946 (amdgpu_crtc->v_border * 2);
Rex Zhuf9e9c082016-03-29 13:21:59 +0800947 mode_info->vblank_time_us = vblank_lines * line_time_us;
948 mode_info->refresh_rate = drm_mode_vrefresh(&amdgpu_crtc->hw_mode);
949 mode_info->ref_clock = adev->clock.spll.reference_freq;
950 mode_info = NULL;
Rex Zhu47bf18b2015-09-17 16:34:14 +0800951 }
952 }
953 }
954
955 return 0;
956}
957
Rex Zhu4c900802016-03-29 14:20:37 +0800958
Dave Airlie110e6f22016-04-12 13:25:48 +1000959static int amdgpu_cgs_notify_dpm_enabled(struct cgs_device *cgs_device, bool enabled)
Rex Zhu4c900802016-03-29 14:20:37 +0800960{
961 CGS_FUNC_ADEV;
962
963 adev->pm.dpm_enabled = enabled;
964
965 return 0;
966}
967
Rex Zhu3f1d35a2015-09-15 14:44:44 +0800968/** \brief evaluate acpi namespace object, handle or pathname must be valid
969 * \param cgs_device
970 * \param info input/output arguments for the control method
971 * \return status
972 */
973
974#if defined(CONFIG_ACPI)
Dave Airlie110e6f22016-04-12 13:25:48 +1000975static int amdgpu_cgs_acpi_eval_object(struct cgs_device *cgs_device,
Rex Zhu3f1d35a2015-09-15 14:44:44 +0800976 struct cgs_acpi_method_info *info)
977{
978 CGS_FUNC_ADEV;
979 acpi_handle handle;
980 struct acpi_object_list input;
981 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Markus Elfring1a8e5f22016-07-16 13:43:44 +0200982 union acpi_object *params, *obj;
Rex Zhu3f1d35a2015-09-15 14:44:44 +0800983 uint8_t name[5] = {'\0'};
Markus Elfringeb09d7a2016-07-16 14:54:12 +0200984 struct cgs_acpi_method_argument *argument;
Rex Zhu3f1d35a2015-09-15 14:44:44 +0800985 uint32_t i, count;
986 acpi_status status;
Markus Elfringb4fc5972016-07-16 15:05:45 +0200987 int result;
Rex Zhu3f1d35a2015-09-15 14:44:44 +0800988
989 handle = ACPI_HANDLE(&adev->pdev->dev);
990 if (!handle)
991 return -ENODEV;
992
993 memset(&input, 0, sizeof(struct acpi_object_list));
994
995 /* validate input info */
996 if (info->size != sizeof(struct cgs_acpi_method_info))
997 return -EINVAL;
998
999 input.count = info->input_count;
1000 if (info->input_count > 0) {
1001 if (info->pinput_argument == NULL)
1002 return -EINVAL;
Dan Carpenterb92c26d2016-01-04 23:43:47 +03001003 argument = info->pinput_argument;
Dan Carpenterb92c26d2016-01-04 23:43:47 +03001004 for (i = 0; i < info->input_count; i++) {
1005 if (((argument->type == ACPI_TYPE_STRING) ||
1006 (argument->type == ACPI_TYPE_BUFFER)) &&
1007 (argument->pointer == NULL))
1008 return -EINVAL;
1009 argument++;
1010 }
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001011 }
1012
1013 if (info->output_count > 0) {
1014 if (info->poutput_argument == NULL)
1015 return -EINVAL;
1016 argument = info->poutput_argument;
1017 for (i = 0; i < info->output_count; i++) {
1018 if (((argument->type == ACPI_TYPE_STRING) ||
1019 (argument->type == ACPI_TYPE_BUFFER))
1020 && (argument->pointer == NULL))
1021 return -EINVAL;
1022 argument++;
1023 }
1024 }
1025
1026 /* The path name passed to acpi_evaluate_object should be null terminated */
1027 if ((info->field & CGS_ACPI_FIELD_METHOD_NAME) != 0) {
1028 strncpy(name, (char *)&(info->name), sizeof(uint32_t));
1029 name[4] = '\0';
1030 }
1031
1032 /* parse input parameters */
1033 if (input.count > 0) {
1034 input.pointer = params =
1035 kzalloc(sizeof(union acpi_object) * input.count, GFP_KERNEL);
1036 if (params == NULL)
1037 return -EINVAL;
1038
1039 argument = info->pinput_argument;
1040
1041 for (i = 0; i < input.count; i++) {
1042 params->type = argument->type;
1043 switch (params->type) {
1044 case ACPI_TYPE_INTEGER:
1045 params->integer.value = argument->value;
1046 break;
1047 case ACPI_TYPE_STRING:
Nicolai Hähnle8db6f832016-06-14 12:10:07 +02001048 params->string.length = argument->data_length;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001049 params->string.pointer = argument->pointer;
1050 break;
1051 case ACPI_TYPE_BUFFER:
Nicolai Hähnle8db6f832016-06-14 12:10:07 +02001052 params->buffer.length = argument->data_length;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001053 params->buffer.pointer = argument->pointer;
1054 break;
1055 default:
1056 break;
1057 }
1058 params++;
1059 argument++;
1060 }
1061 }
1062
1063 /* parse output info */
1064 count = info->output_count;
1065 argument = info->poutput_argument;
1066
1067 /* evaluate the acpi method */
1068 status = acpi_evaluate_object(handle, name, &input, &output);
1069
1070 if (ACPI_FAILURE(status)) {
1071 result = -EIO;
Markus Elfring1a8e5f22016-07-16 13:43:44 +02001072 goto free_input;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001073 }
1074
1075 /* return the output info */
1076 obj = output.pointer;
1077
1078 if (count > 1) {
1079 if ((obj->type != ACPI_TYPE_PACKAGE) ||
1080 (obj->package.count != count)) {
1081 result = -EIO;
Markus Elfring1a8e5f22016-07-16 13:43:44 +02001082 goto free_obj;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001083 }
1084 params = obj->package.elements;
1085 } else
1086 params = obj;
1087
1088 if (params == NULL) {
1089 result = -EIO;
Markus Elfring1a8e5f22016-07-16 13:43:44 +02001090 goto free_obj;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001091 }
1092
1093 for (i = 0; i < count; i++) {
1094 if (argument->type != params->type) {
1095 result = -EIO;
Markus Elfring1a8e5f22016-07-16 13:43:44 +02001096 goto free_obj;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001097 }
1098 switch (params->type) {
1099 case ACPI_TYPE_INTEGER:
1100 argument->value = params->integer.value;
1101 break;
1102 case ACPI_TYPE_STRING:
1103 if ((params->string.length != argument->data_length) ||
1104 (params->string.pointer == NULL)) {
1105 result = -EIO;
Markus Elfring1a8e5f22016-07-16 13:43:44 +02001106 goto free_obj;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001107 }
1108 strncpy(argument->pointer,
1109 params->string.pointer,
1110 params->string.length);
1111 break;
1112 case ACPI_TYPE_BUFFER:
1113 if (params->buffer.pointer == NULL) {
1114 result = -EIO;
Markus Elfring1a8e5f22016-07-16 13:43:44 +02001115 goto free_obj;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001116 }
1117 memcpy(argument->pointer,
1118 params->buffer.pointer,
1119 argument->data_length);
1120 break;
1121 default:
1122 break;
1123 }
1124 argument++;
1125 params++;
1126 }
1127
Markus Elfringb4fc5972016-07-16 15:05:45 +02001128 result = 0;
Markus Elfring1a8e5f22016-07-16 13:43:44 +02001129free_obj:
Edward O'Callaghana698e412016-07-12 10:17:54 +10001130 kfree(obj);
Markus Elfring1a8e5f22016-07-16 13:43:44 +02001131free_input:
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001132 kfree((void *)input.pointer);
1133 return result;
1134}
1135#else
Dave Airlie110e6f22016-04-12 13:25:48 +10001136static int amdgpu_cgs_acpi_eval_object(struct cgs_device *cgs_device,
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001137 struct cgs_acpi_method_info *info)
1138{
1139 return -EIO;
1140}
1141#endif
1142
Huang Ruieadf9542016-07-16 13:04:22 +08001143static int amdgpu_cgs_call_acpi_method(struct cgs_device *cgs_device,
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001144 uint32_t acpi_method,
1145 uint32_t acpi_function,
1146 void *pinput, void *poutput,
1147 uint32_t output_count,
1148 uint32_t input_size,
1149 uint32_t output_size)
1150{
1151 struct cgs_acpi_method_argument acpi_input[2] = { {0}, {0} };
1152 struct cgs_acpi_method_argument acpi_output = {0};
1153 struct cgs_acpi_method_info info = {0};
1154
1155 acpi_input[0].type = CGS_ACPI_TYPE_INTEGER;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001156 acpi_input[0].data_length = sizeof(uint32_t);
1157 acpi_input[0].value = acpi_function;
1158
1159 acpi_input[1].type = CGS_ACPI_TYPE_BUFFER;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001160 acpi_input[1].data_length = input_size;
1161 acpi_input[1].pointer = pinput;
1162
1163 acpi_output.type = CGS_ACPI_TYPE_BUFFER;
Rex Zhu3f1d35a2015-09-15 14:44:44 +08001164 acpi_output.data_length = output_size;
1165 acpi_output.pointer = poutput;
1166
1167 info.size = sizeof(struct cgs_acpi_method_info);
1168 info.field = CGS_ACPI_FIELD_METHOD_NAME | CGS_ACPI_FIELD_INPUT_ARGUMENT_COUNT;
1169 info.input_count = 2;
1170 info.name = acpi_method;
1171 info.pinput_argument = acpi_input;
1172 info.output_count = output_count;
1173 info.poutput_argument = &acpi_output;
1174
1175 return amdgpu_cgs_acpi_eval_object(cgs_device, &info);
1176}
1177
Chunming Zhoud03846a2015-07-28 14:20:03 -04001178static const struct cgs_ops amdgpu_cgs_ops = {
Kees Cook613e61a2016-12-16 17:02:32 -08001179 .alloc_gpu_mem = amdgpu_cgs_alloc_gpu_mem,
1180 .free_gpu_mem = amdgpu_cgs_free_gpu_mem,
1181 .gmap_gpu_mem = amdgpu_cgs_gmap_gpu_mem,
1182 .gunmap_gpu_mem = amdgpu_cgs_gunmap_gpu_mem,
1183 .kmap_gpu_mem = amdgpu_cgs_kmap_gpu_mem,
1184 .kunmap_gpu_mem = amdgpu_cgs_kunmap_gpu_mem,
1185 .read_register = amdgpu_cgs_read_register,
1186 .write_register = amdgpu_cgs_write_register,
1187 .read_ind_register = amdgpu_cgs_read_ind_register,
1188 .write_ind_register = amdgpu_cgs_write_ind_register,
Kees Cook613e61a2016-12-16 17:02:32 -08001189 .get_pci_resource = amdgpu_cgs_get_pci_resource,
1190 .atom_get_data_table = amdgpu_cgs_atom_get_data_table,
1191 .atom_get_cmd_table_revs = amdgpu_cgs_atom_get_cmd_table_revs,
1192 .atom_exec_cmd_table = amdgpu_cgs_atom_exec_cmd_table,
Kees Cook613e61a2016-12-16 17:02:32 -08001193 .get_firmware_info = amdgpu_cgs_get_firmware_info,
1194 .rel_firmware = amdgpu_cgs_rel_firmware,
1195 .set_powergating_state = amdgpu_cgs_set_powergating_state,
1196 .set_clockgating_state = amdgpu_cgs_set_clockgating_state,
1197 .get_active_displays_info = amdgpu_cgs_get_active_displays_info,
1198 .notify_dpm_enabled = amdgpu_cgs_notify_dpm_enabled,
1199 .call_acpi_method = amdgpu_cgs_call_acpi_method,
1200 .query_system_info = amdgpu_cgs_query_system_info,
1201 .is_virtualization_enabled = amdgpu_cgs_is_virtualization_enabled,
Rex Zhue8a95b22016-12-21 20:30:58 +08001202 .enter_safe_mode = amdgpu_cgs_enter_safe_mode,
Evan Quan209ee272017-07-04 15:37:09 +08001203 .lock_grbm_idx = amdgpu_cgs_lock_grbm_idx,
Rex Zhuba89a3e2017-09-25 20:45:52 +08001204 .register_pp_handle = amdgpu_cgs_register_pp_handle,
Chunming Zhoud03846a2015-07-28 14:20:03 -04001205};
1206
1207static const struct cgs_os_ops amdgpu_cgs_os_ops = {
Kees Cook613e61a2016-12-16 17:02:32 -08001208 .add_irq_source = amdgpu_cgs_add_irq_source,
1209 .irq_get = amdgpu_cgs_irq_get,
1210 .irq_put = amdgpu_cgs_irq_put
Chunming Zhoud03846a2015-07-28 14:20:03 -04001211};
1212
Dave Airlie110e6f22016-04-12 13:25:48 +10001213struct cgs_device *amdgpu_cgs_create_device(struct amdgpu_device *adev)
Chunming Zhoud03846a2015-07-28 14:20:03 -04001214{
1215 struct amdgpu_cgs_device *cgs_device =
1216 kmalloc(sizeof(*cgs_device), GFP_KERNEL);
1217
1218 if (!cgs_device) {
1219 DRM_ERROR("Couldn't allocate CGS device structure\n");
1220 return NULL;
1221 }
1222
1223 cgs_device->base.ops = &amdgpu_cgs_ops;
1224 cgs_device->base.os_ops = &amdgpu_cgs_os_ops;
1225 cgs_device->adev = adev;
1226
Dave Airlie110e6f22016-04-12 13:25:48 +10001227 return (struct cgs_device *)cgs_device;
Chunming Zhoud03846a2015-07-28 14:20:03 -04001228}
1229
Dave Airlie110e6f22016-04-12 13:25:48 +10001230void amdgpu_cgs_destroy_device(struct cgs_device *cgs_device)
Chunming Zhoud03846a2015-07-28 14:20:03 -04001231{
1232 kfree(cgs_device);
1233}