2 * Copyright 2015 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <linux/list.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/acpi.h>
29 #include <linux/firmware.h>
30 #include <drm/amdgpu_drm.h>
32 #include "cgs_linux.h"
34 #include "amdgpu_ucode.h"
36 struct amdgpu_cgs_device {
37 struct cgs_device base;
38 struct amdgpu_device *adev;
41 #define CGS_FUNC_ADEV \
42 struct amdgpu_device *adev = \
43 ((struct amdgpu_cgs_device *)cgs_device)->adev
45 static void *amdgpu_cgs_register_pp_handle(struct cgs_device *cgs_device,
46 int (*call_back_func)(struct amd_pp_init *, void **))
49 struct amd_pp_init pp_init;
50 struct amd_powerplay *amd_pp;
52 if (call_back_func == NULL)
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)))
64 return adev->powerplay.pp_handle;
67 static int amdgpu_cgs_alloc_gpu_mem(struct cgs_device *cgs_device,
68 enum cgs_gpu_mem_type type,
69 uint64_t size, uint64_t align,
76 struct amdgpu_bo *obj;
78 /* fail if the alignment is not a power of 2 */
79 if (((align != 1) && (align & (align - 1)))
80 || size == 0 || align == 0)
85 case CGS_GPU_MEM_TYPE__VISIBLE_CONTIG_FB:
86 case CGS_GPU_MEM_TYPE__VISIBLE_FB:
87 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
88 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
89 domain = AMDGPU_GEM_DOMAIN_VRAM;
91 case CGS_GPU_MEM_TYPE__INVISIBLE_CONTIG_FB:
92 case CGS_GPU_MEM_TYPE__INVISIBLE_FB:
93 flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
94 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
95 domain = AMDGPU_GEM_DOMAIN_VRAM;
97 case CGS_GPU_MEM_TYPE__GART_CACHEABLE:
98 domain = AMDGPU_GEM_DOMAIN_GTT;
100 case CGS_GPU_MEM_TYPE__GART_WRITECOMBINE:
101 flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
102 domain = AMDGPU_GEM_DOMAIN_GTT;
111 ret = amdgpu_bo_create(adev, size, align, true, domain, flags,
114 DRM_ERROR("(%d) bo create failed\n", ret);
117 *handle = (cgs_handle_t)obj;
122 static int amdgpu_cgs_free_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle)
124 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
127 int r = amdgpu_bo_reserve(obj, true);
128 if (likely(r == 0)) {
129 amdgpu_bo_kunmap(obj);
130 amdgpu_bo_unpin(obj);
131 amdgpu_bo_unreserve(obj);
133 amdgpu_bo_unref(&obj);
139 static int amdgpu_cgs_gmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle,
143 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
145 WARN_ON_ONCE(obj->placement.num_placement > 1);
147 r = amdgpu_bo_reserve(obj, true);
148 if (unlikely(r != 0))
150 r = amdgpu_bo_pin(obj, obj->preferred_domains, mcaddr);
151 amdgpu_bo_unreserve(obj);
155 static int amdgpu_cgs_gunmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle)
158 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
159 r = amdgpu_bo_reserve(obj, true);
160 if (unlikely(r != 0))
162 r = amdgpu_bo_unpin(obj);
163 amdgpu_bo_unreserve(obj);
167 static int amdgpu_cgs_kmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle,
171 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
172 r = amdgpu_bo_reserve(obj, true);
173 if (unlikely(r != 0))
175 r = amdgpu_bo_kmap(obj, map);
176 amdgpu_bo_unreserve(obj);
180 static int amdgpu_cgs_kunmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle)
183 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
184 r = amdgpu_bo_reserve(obj, true);
185 if (unlikely(r != 0))
187 amdgpu_bo_kunmap(obj);
188 amdgpu_bo_unreserve(obj);
192 static uint32_t amdgpu_cgs_read_register(struct cgs_device *cgs_device, unsigned offset)
195 return RREG32(offset);
198 static void amdgpu_cgs_write_register(struct cgs_device *cgs_device, unsigned offset,
202 WREG32(offset, value);
205 static uint32_t amdgpu_cgs_read_ind_register(struct cgs_device *cgs_device,
206 enum cgs_ind_reg 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);
221 case CGS_IND_REG_GC_CAC:
222 return RREG32_GC_CAC(index);
223 case CGS_IND_REG_SE_CAC:
224 return RREG32_SE_CAC(index);
225 case CGS_IND_REG__AUDIO_ENDPT:
226 DRM_ERROR("audio endpt register access not implemented.\n");
229 WARN(1, "Invalid indirect register space");
233 static void amdgpu_cgs_write_ind_register(struct cgs_device *cgs_device,
234 enum cgs_ind_reg space,
235 unsigned index, uint32_t value)
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);
249 case CGS_IND_REG_GC_CAC:
250 return WREG32_GC_CAC(index, value);
251 case CGS_IND_REG_SE_CAC:
252 return WREG32_SE_CAC(index, value);
253 case CGS_IND_REG__AUDIO_ENDPT:
254 DRM_ERROR("audio endpt register access not implemented.\n");
257 WARN(1, "Invalid indirect register space");
260 static int amdgpu_cgs_get_pci_resource(struct cgs_device *cgs_device,
261 enum cgs_resource_type resource_type,
264 uint64_t *resource_base)
268 if (resource_base == NULL)
271 switch (resource_type) {
272 case CGS_RESOURCE_TYPE_MMIO:
273 if (adev->rmmio_size == 0)
275 if ((offset + size) > adev->rmmio_size)
277 *resource_base = adev->rmmio_base;
279 case CGS_RESOURCE_TYPE_DOORBELL:
280 if (adev->doorbell.size == 0)
282 if ((offset + size) > adev->doorbell.size)
284 *resource_base = adev->doorbell.base;
286 case CGS_RESOURCE_TYPE_FB:
287 case CGS_RESOURCE_TYPE_IO:
288 case CGS_RESOURCE_TYPE_ROM:
294 static const void *amdgpu_cgs_atom_get_data_table(struct cgs_device *cgs_device,
295 unsigned table, uint16_t *size,
296 uint8_t *frev, uint8_t *crev)
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 +
310 static int amdgpu_cgs_atom_get_cmd_table_revs(struct cgs_device *cgs_device, unsigned table,
311 uint8_t *frev, uint8_t *crev)
315 if (amdgpu_atom_parse_cmd_header(
316 adev->mode_info.atom_context, table,
323 static int amdgpu_cgs_atom_exec_cmd_table(struct cgs_device *cgs_device, unsigned table,
328 return amdgpu_atom_execute_table(
329 adev->mode_info.atom_context, table, args);
332 struct cgs_irq_params {
334 cgs_irq_source_set_func_t set;
335 cgs_irq_handler_func_t handler;
339 static int cgs_set_irq_state(struct amdgpu_device *adev,
340 struct amdgpu_irq_src *src,
342 enum amdgpu_interrupt_state state)
344 struct cgs_irq_params *irq_params =
345 (struct cgs_irq_params *)src->data;
348 if (!irq_params->set)
350 return irq_params->set(irq_params->private_data,
356 static int cgs_process_irq(struct amdgpu_device *adev,
357 struct amdgpu_irq_src *source,
358 struct amdgpu_iv_entry *entry)
360 struct cgs_irq_params *irq_params =
361 (struct cgs_irq_params *)source->data;
364 if (!irq_params->handler)
366 return irq_params->handler(irq_params->private_data,
371 static const struct amdgpu_irq_src_funcs cgs_irq_funcs = {
372 .set = cgs_set_irq_state,
373 .process = cgs_process_irq,
376 static int amdgpu_cgs_add_irq_source(void *cgs_device,
380 cgs_irq_source_set_func_t set,
381 cgs_irq_handler_func_t handler,
386 struct cgs_irq_params *irq_params;
387 struct amdgpu_irq_src *source =
388 kzalloc(sizeof(struct amdgpu_irq_src), GFP_KERNEL);
392 kzalloc(sizeof(struct cgs_irq_params), GFP_KERNEL);
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;
404 ret = amdgpu_irq_add_id(adev, client_id, src_id, source);
413 static int amdgpu_cgs_irq_get(void *cgs_device, unsigned client_id,
414 unsigned src_id, unsigned type)
418 if (!adev->irq.client[client_id].sources)
421 return amdgpu_irq_get(adev, adev->irq.client[client_id].sources[src_id], type);
424 static int amdgpu_cgs_irq_put(void *cgs_device, unsigned client_id,
425 unsigned src_id, unsigned type)
429 if (!adev->irq.client[client_id].sources)
432 return amdgpu_irq_put(adev, adev->irq.client[client_id].sources[src_id], type);
435 static int amdgpu_cgs_set_clockgating_state(struct cgs_device *cgs_device,
436 enum amd_ip_block_type block_type,
437 enum amd_clockgating_state state)
442 for (i = 0; i < adev->num_ip_blocks; i++) {
443 if (!adev->ip_blocks[i].status.valid)
446 if (adev->ip_blocks[i].version->type == block_type) {
447 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
456 static int amdgpu_cgs_set_powergating_state(struct cgs_device *cgs_device,
457 enum amd_ip_block_type block_type,
458 enum amd_powergating_state state)
463 for (i = 0; i < adev->num_ip_blocks; i++) {
464 if (!adev->ip_blocks[i].status.valid)
467 if (adev->ip_blocks[i].version->type == block_type) {
468 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
478 static uint32_t fw_type_convert(struct cgs_device *cgs_device, uint32_t fw_type)
481 enum AMDGPU_UCODE_ID result = AMDGPU_UCODE_ID_MAXIMUM;
484 case CGS_UCODE_ID_SDMA0:
485 result = AMDGPU_UCODE_ID_SDMA0;
487 case CGS_UCODE_ID_SDMA1:
488 result = AMDGPU_UCODE_ID_SDMA1;
490 case CGS_UCODE_ID_CP_CE:
491 result = AMDGPU_UCODE_ID_CP_CE;
493 case CGS_UCODE_ID_CP_PFP:
494 result = AMDGPU_UCODE_ID_CP_PFP;
496 case CGS_UCODE_ID_CP_ME:
497 result = AMDGPU_UCODE_ID_CP_ME;
499 case CGS_UCODE_ID_CP_MEC:
500 case CGS_UCODE_ID_CP_MEC_JT1:
501 result = AMDGPU_UCODE_ID_CP_MEC1;
503 case CGS_UCODE_ID_CP_MEC_JT2:
504 /* 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.
508 if (adev->asic_type >= CHIP_TOPAZ)
509 result = AMDGPU_UCODE_ID_CP_MEC1;
511 result = AMDGPU_UCODE_ID_CP_MEC2;
513 case CGS_UCODE_ID_RLC_G:
514 result = AMDGPU_UCODE_ID_RLC_G;
516 case CGS_UCODE_ID_STORAGE:
517 result = AMDGPU_UCODE_ID_STORAGE;
520 DRM_ERROR("Firmware type not supported\n");
525 static int amdgpu_cgs_rel_firmware(struct cgs_device *cgs_device, enum cgs_ucode_id type)
528 if ((CGS_UCODE_ID_SMU == type) || (CGS_UCODE_ID_SMU_SK == type)) {
529 release_firmware(adev->pm.fw);
533 /* cannot release other firmware because they are not created by cgs */
537 static uint16_t amdgpu_get_firmware_version(struct cgs_device *cgs_device,
538 enum cgs_ucode_id type)
541 uint16_t fw_version = 0;
544 case CGS_UCODE_ID_SDMA0:
545 fw_version = adev->sdma.instance[0].fw_version;
547 case CGS_UCODE_ID_SDMA1:
548 fw_version = adev->sdma.instance[1].fw_version;
550 case CGS_UCODE_ID_CP_CE:
551 fw_version = adev->gfx.ce_fw_version;
553 case CGS_UCODE_ID_CP_PFP:
554 fw_version = adev->gfx.pfp_fw_version;
556 case CGS_UCODE_ID_CP_ME:
557 fw_version = adev->gfx.me_fw_version;
559 case CGS_UCODE_ID_CP_MEC:
560 fw_version = adev->gfx.mec_fw_version;
562 case CGS_UCODE_ID_CP_MEC_JT1:
563 fw_version = adev->gfx.mec_fw_version;
565 case CGS_UCODE_ID_CP_MEC_JT2:
566 fw_version = adev->gfx.mec_fw_version;
568 case CGS_UCODE_ID_RLC_G:
569 fw_version = adev->gfx.rlc_fw_version;
571 case CGS_UCODE_ID_STORAGE:
574 DRM_ERROR("firmware type %d do not have version\n", type);
580 static int amdgpu_cgs_enter_safe_mode(struct cgs_device *cgs_device,
585 if (adev->gfx.rlc.funcs->enter_safe_mode == NULL ||
586 adev->gfx.rlc.funcs->exit_safe_mode == NULL)
590 adev->gfx.rlc.funcs->enter_safe_mode(adev);
592 adev->gfx.rlc.funcs->exit_safe_mode(adev);
597 static void amdgpu_cgs_lock_grbm_idx(struct cgs_device *cgs_device,
603 mutex_lock(&adev->grbm_idx_mutex);
605 mutex_unlock(&adev->grbm_idx_mutex);
608 static int amdgpu_cgs_get_firmware_info(struct cgs_device *cgs_device,
609 enum cgs_ucode_id type,
610 struct cgs_firmware_info *info)
614 if ((CGS_UCODE_ID_SMU != type) && (CGS_UCODE_ID_SMU_SK != type)) {
617 const struct gfx_firmware_header_v1_0 *header;
618 enum AMDGPU_UCODE_ID id;
619 struct amdgpu_firmware_info *ucode;
621 id = fw_type_convert(cgs_device, type);
622 ucode = &adev->firmware.ucode[id];
623 if (ucode->fw == NULL)
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);
630 if ((type == CGS_UCODE_ID_CP_MEC_JT1) ||
631 (type == CGS_UCODE_ID_CP_MEC_JT2)) {
632 gpu_addr += ALIGN(le32_to_cpu(header->header.ucode_size_bytes), PAGE_SIZE);
633 data_size = le32_to_cpu(header->jt_size) << 2;
636 info->kptr = ucode->kaddr;
637 info->image_size = data_size;
638 info->mc_addr = gpu_addr;
639 info->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
641 if (CGS_UCODE_ID_CP_MEC == type)
642 info->image_size = le32_to_cpu(header->jt_offset) << 2;
644 info->fw_version = amdgpu_get_firmware_version(cgs_device, type);
645 info->feature_version = (uint16_t)le32_to_cpu(header->ucode_feature_version);
647 char fw_name[30] = {0};
650 uint32_t ucode_start_address;
652 const struct smc_firmware_header_v1_0 *hdr;
653 const struct common_firmware_header *header;
654 struct amdgpu_firmware_info *ucode = NULL;
657 switch (adev->asic_type) {
659 strcpy(fw_name, "radeon/tahiti_smc.bin");
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");
668 strcpy(fw_name, "radeon/pitcairn_smc.bin");
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");
684 strcpy(fw_name, "radeon/verde_smc.bin");
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");
698 strcpy(fw_name, "radeon/oland_smc.bin");
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");
716 strcpy(fw_name, "radeon/hainan_smc.bin");
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");
726 strcpy(fw_name, "radeon/bonaire_smc.bin");
730 if (adev->pdev->revision == 0x80) {
731 info->is_kicker = true;
732 strcpy(fw_name, "radeon/hawaii_k_smc.bin");
734 strcpy(fw_name, "radeon/hawaii_smc.bin");
738 if (((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x81)) ||
739 ((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x83)) ||
740 ((adev->pdev->device == 0x6907) && (adev->pdev->revision == 0x87))) {
741 info->is_kicker = true;
742 strcpy(fw_name, "amdgpu/topaz_k_smc.bin");
744 strcpy(fw_name, "amdgpu/topaz_smc.bin");
747 if (((adev->pdev->device == 0x6939) && (adev->pdev->revision == 0xf1)) ||
748 ((adev->pdev->device == 0x6938) && (adev->pdev->revision == 0xf1))) {
749 info->is_kicker = true;
750 strcpy(fw_name, "amdgpu/tonga_k_smc.bin");
752 strcpy(fw_name, "amdgpu/tonga_smc.bin");
755 strcpy(fw_name, "amdgpu/fiji_smc.bin");
758 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) ||
766 (adev->pdev->revision == 0xff)))) {
767 info->is_kicker = true;
768 strcpy(fw_name, "amdgpu/polaris11_k_smc.bin");
770 strcpy(fw_name, "amdgpu/polaris11_smc.bin");
771 } else if (type == CGS_UCODE_ID_SMU_SK) {
772 strcpy(fw_name, "amdgpu/polaris11_smc_sk.bin");
776 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) ||
783 (adev->pdev->revision == 0xef))) {
784 info->is_kicker = true;
785 strcpy(fw_name, "amdgpu/polaris10_k_smc.bin");
787 strcpy(fw_name, "amdgpu/polaris10_smc.bin");
788 } else if (type == CGS_UCODE_ID_SMU_SK) {
789 strcpy(fw_name, "amdgpu/polaris10_smc_sk.bin");
793 strcpy(fw_name, "amdgpu/polaris12_smc.bin");
796 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");
802 strcpy(fw_name, "amdgpu/vega10_smc.bin");
807 adev->pm.fw_version = info->version;
810 DRM_ERROR("SMC firmware not supported\n");
814 err = request_firmware(&adev->pm.fw, fw_name, adev->dev);
816 DRM_ERROR("Failed to request firmware\n");
820 err = amdgpu_ucode_validate(adev->pm.fw);
822 DRM_ERROR("Failed to load firmware \"%s\"", fw_name);
823 release_firmware(adev->pm.fw);
828 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
829 ucode = &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
830 ucode->ucode_id = AMDGPU_UCODE_ID_SMC;
831 ucode->fw = adev->pm.fw;
832 header = (const struct common_firmware_header *)ucode->fw->data;
833 adev->firmware.fw_size +=
834 ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
838 hdr = (const struct smc_firmware_header_v1_0 *) adev->pm.fw->data;
839 amdgpu_ucode_print_smc_hdr(&hdr->header);
840 adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
841 ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
842 ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
843 src = (const uint8_t *)(adev->pm.fw->data +
844 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
846 info->version = adev->pm.fw_version;
847 info->image_size = ucode_size;
848 info->ucode_start_address = ucode_start_address;
849 info->kptr = (void *)src;
854 static int amdgpu_cgs_is_virtualization_enabled(void *cgs_device)
857 return amdgpu_sriov_vf(adev);
860 static int amdgpu_cgs_query_system_info(struct cgs_device *cgs_device,
861 struct cgs_system_info *sys_info)
865 if (NULL == sys_info)
868 if (sizeof(struct cgs_system_info) != sys_info->size)
871 switch (sys_info->info_id) {
872 case CGS_SYSTEM_INFO_ADAPTER_BDF_ID:
873 sys_info->value = adev->pdev->devfn | (adev->pdev->bus->number << 8);
875 case CGS_SYSTEM_INFO_PCIE_GEN_INFO:
876 sys_info->value = adev->pm.pcie_gen_mask;
878 case CGS_SYSTEM_INFO_PCIE_MLW:
879 sys_info->value = adev->pm.pcie_mlw_mask;
881 case CGS_SYSTEM_INFO_PCIE_DEV:
882 sys_info->value = adev->pdev->device;
884 case CGS_SYSTEM_INFO_PCIE_REV:
885 sys_info->value = adev->pdev->revision;
887 case CGS_SYSTEM_INFO_CG_FLAGS:
888 sys_info->value = adev->cg_flags;
890 case CGS_SYSTEM_INFO_PG_FLAGS:
891 sys_info->value = adev->pg_flags;
893 case CGS_SYSTEM_INFO_GFX_CU_INFO:
894 sys_info->value = adev->gfx.cu_info.number;
896 case CGS_SYSTEM_INFO_GFX_SE_INFO:
897 sys_info->value = adev->gfx.config.max_shader_engines;
899 case CGS_SYSTEM_INFO_PCIE_SUB_SYS_ID:
900 sys_info->value = adev->pdev->subsystem_device;
902 case CGS_SYSTEM_INFO_PCIE_SUB_SYS_VENDOR_ID:
903 sys_info->value = adev->pdev->subsystem_vendor;
905 case CGS_SYSTEM_INFO_PCIE_BUS_DEVFN:
906 sys_info->value = adev->pdev->devfn;
908 case CGS_SYSTEM_INFO_VRAM_WIDTH:
909 sys_info->value = adev->gmc.vram_width;
918 static int amdgpu_cgs_get_active_displays_info(struct cgs_device *cgs_device,
919 struct cgs_display_info *info)
922 struct cgs_mode_info *mode_info;
927 mode_info = info->mode_info;
929 /* if the displays are off, vblank time is max */
930 mode_info->vblank_time_us = 0xffffffff;
931 /* always set the reference clock */
932 mode_info->ref_clock = adev->clock.spll.reference_freq;
935 if (!amdgpu_device_has_dc_support(adev)) {
936 struct amdgpu_crtc *amdgpu_crtc;
937 struct drm_device *ddev = adev->ddev;
938 struct drm_crtc *crtc;
939 uint32_t line_time_us, vblank_lines;
941 if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
942 list_for_each_entry(crtc,
943 &ddev->mode_config.crtc_list, head) {
944 amdgpu_crtc = to_amdgpu_crtc(crtc);
946 info->active_display_mask |= (1 << amdgpu_crtc->crtc_id);
947 info->display_count++;
949 if (mode_info != NULL &&
950 crtc->enabled && amdgpu_crtc->enabled &&
951 amdgpu_crtc->hw_mode.clock) {
952 line_time_us = (amdgpu_crtc->hw_mode.crtc_htotal * 1000) /
953 amdgpu_crtc->hw_mode.clock;
954 vblank_lines = amdgpu_crtc->hw_mode.crtc_vblank_end -
955 amdgpu_crtc->hw_mode.crtc_vdisplay +
956 (amdgpu_crtc->v_border * 2);
957 mode_info->vblank_time_us = vblank_lines * line_time_us;
958 mode_info->refresh_rate = drm_mode_vrefresh(&amdgpu_crtc->hw_mode);
959 /* we have issues with mclk switching with refresh rates
960 * over 120 hz on the non-DC code.
962 if (mode_info->refresh_rate > 120)
963 mode_info->vblank_time_us = 0;
969 info->display_count = adev->pm.pm_display_cfg.num_display;
970 if (mode_info != NULL) {
971 mode_info->vblank_time_us = adev->pm.pm_display_cfg.min_vblank_time;
972 mode_info->refresh_rate = adev->pm.pm_display_cfg.vrefresh;
979 static int amdgpu_cgs_notify_dpm_enabled(struct cgs_device *cgs_device, bool enabled)
983 adev->pm.dpm_enabled = enabled;
988 /** \brief evaluate acpi namespace object, handle or pathname must be valid
990 * \param info input/output arguments for the control method
994 #if defined(CONFIG_ACPI)
995 static int amdgpu_cgs_acpi_eval_object(struct cgs_device *cgs_device,
996 struct cgs_acpi_method_info *info)
1000 struct acpi_object_list input;
1001 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1002 union acpi_object *params, *obj;
1003 uint8_t name[5] = {'\0'};
1004 struct cgs_acpi_method_argument *argument;
1009 handle = ACPI_HANDLE(&adev->pdev->dev);
1013 memset(&input, 0, sizeof(struct acpi_object_list));
1015 /* validate input info */
1016 if (info->size != sizeof(struct cgs_acpi_method_info))
1019 input.count = info->input_count;
1020 if (info->input_count > 0) {
1021 if (info->pinput_argument == NULL)
1023 argument = info->pinput_argument;
1024 for (i = 0; i < info->input_count; i++) {
1025 if (((argument->type == ACPI_TYPE_STRING) ||
1026 (argument->type == ACPI_TYPE_BUFFER)) &&
1027 (argument->pointer == NULL))
1033 if (info->output_count > 0) {
1034 if (info->poutput_argument == NULL)
1036 argument = info->poutput_argument;
1037 for (i = 0; i < info->output_count; i++) {
1038 if (((argument->type == ACPI_TYPE_STRING) ||
1039 (argument->type == ACPI_TYPE_BUFFER))
1040 && (argument->pointer == NULL))
1046 /* The path name passed to acpi_evaluate_object should be null terminated */
1047 if ((info->field & CGS_ACPI_FIELD_METHOD_NAME) != 0) {
1048 strncpy(name, (char *)&(info->name), sizeof(uint32_t));
1052 /* parse input parameters */
1053 if (input.count > 0) {
1054 input.pointer = params =
1055 kzalloc(sizeof(union acpi_object) * input.count, GFP_KERNEL);
1059 argument = info->pinput_argument;
1061 for (i = 0; i < input.count; i++) {
1062 params->type = argument->type;
1063 switch (params->type) {
1064 case ACPI_TYPE_INTEGER:
1065 params->integer.value = argument->value;
1067 case ACPI_TYPE_STRING:
1068 params->string.length = argument->data_length;
1069 params->string.pointer = argument->pointer;
1071 case ACPI_TYPE_BUFFER:
1072 params->buffer.length = argument->data_length;
1073 params->buffer.pointer = argument->pointer;
1083 /* parse output info */
1084 count = info->output_count;
1085 argument = info->poutput_argument;
1087 /* evaluate the acpi method */
1088 status = acpi_evaluate_object(handle, name, &input, &output);
1090 if (ACPI_FAILURE(status)) {
1095 /* return the output info */
1096 obj = output.pointer;
1099 if ((obj->type != ACPI_TYPE_PACKAGE) ||
1100 (obj->package.count != count)) {
1104 params = obj->package.elements;
1108 if (params == NULL) {
1113 for (i = 0; i < count; i++) {
1114 if (argument->type != params->type) {
1118 switch (params->type) {
1119 case ACPI_TYPE_INTEGER:
1120 argument->value = params->integer.value;
1122 case ACPI_TYPE_STRING:
1123 if ((params->string.length != argument->data_length) ||
1124 (params->string.pointer == NULL)) {
1128 strncpy(argument->pointer,
1129 params->string.pointer,
1130 params->string.length);
1132 case ACPI_TYPE_BUFFER:
1133 if (params->buffer.pointer == NULL) {
1137 memcpy(argument->pointer,
1138 params->buffer.pointer,
1139 argument->data_length);
1152 kfree((void *)input.pointer);
1156 static int amdgpu_cgs_acpi_eval_object(struct cgs_device *cgs_device,
1157 struct cgs_acpi_method_info *info)
1163 static int amdgpu_cgs_call_acpi_method(struct cgs_device *cgs_device,
1164 uint32_t acpi_method,
1165 uint32_t acpi_function,
1166 void *pinput, void *poutput,
1167 uint32_t output_count,
1168 uint32_t input_size,
1169 uint32_t output_size)
1171 struct cgs_acpi_method_argument acpi_input[2] = { {0}, {0} };
1172 struct cgs_acpi_method_argument acpi_output = {0};
1173 struct cgs_acpi_method_info info = {0};
1175 acpi_input[0].type = CGS_ACPI_TYPE_INTEGER;
1176 acpi_input[0].data_length = sizeof(uint32_t);
1177 acpi_input[0].value = acpi_function;
1179 acpi_input[1].type = CGS_ACPI_TYPE_BUFFER;
1180 acpi_input[1].data_length = input_size;
1181 acpi_input[1].pointer = pinput;
1183 acpi_output.type = CGS_ACPI_TYPE_BUFFER;
1184 acpi_output.data_length = output_size;
1185 acpi_output.pointer = poutput;
1187 info.size = sizeof(struct cgs_acpi_method_info);
1188 info.field = CGS_ACPI_FIELD_METHOD_NAME | CGS_ACPI_FIELD_INPUT_ARGUMENT_COUNT;
1189 info.input_count = 2;
1190 info.name = acpi_method;
1191 info.pinput_argument = acpi_input;
1192 info.output_count = output_count;
1193 info.poutput_argument = &acpi_output;
1195 return amdgpu_cgs_acpi_eval_object(cgs_device, &info);
1198 static int amdgpu_cgs_set_temperature_range(struct cgs_device *cgs_device,
1199 int min_temperature,
1200 int max_temperature)
1204 adev->pm.dpm.thermal.min_temp = min_temperature;
1205 adev->pm.dpm.thermal.max_temp = max_temperature;
1210 static const struct cgs_ops amdgpu_cgs_ops = {
1211 .alloc_gpu_mem = amdgpu_cgs_alloc_gpu_mem,
1212 .free_gpu_mem = amdgpu_cgs_free_gpu_mem,
1213 .gmap_gpu_mem = amdgpu_cgs_gmap_gpu_mem,
1214 .gunmap_gpu_mem = amdgpu_cgs_gunmap_gpu_mem,
1215 .kmap_gpu_mem = amdgpu_cgs_kmap_gpu_mem,
1216 .kunmap_gpu_mem = amdgpu_cgs_kunmap_gpu_mem,
1217 .read_register = amdgpu_cgs_read_register,
1218 .write_register = amdgpu_cgs_write_register,
1219 .read_ind_register = amdgpu_cgs_read_ind_register,
1220 .write_ind_register = amdgpu_cgs_write_ind_register,
1221 .get_pci_resource = amdgpu_cgs_get_pci_resource,
1222 .atom_get_data_table = amdgpu_cgs_atom_get_data_table,
1223 .atom_get_cmd_table_revs = amdgpu_cgs_atom_get_cmd_table_revs,
1224 .atom_exec_cmd_table = amdgpu_cgs_atom_exec_cmd_table,
1225 .get_firmware_info = amdgpu_cgs_get_firmware_info,
1226 .rel_firmware = amdgpu_cgs_rel_firmware,
1227 .set_powergating_state = amdgpu_cgs_set_powergating_state,
1228 .set_clockgating_state = amdgpu_cgs_set_clockgating_state,
1229 .get_active_displays_info = amdgpu_cgs_get_active_displays_info,
1230 .notify_dpm_enabled = amdgpu_cgs_notify_dpm_enabled,
1231 .call_acpi_method = amdgpu_cgs_call_acpi_method,
1232 .query_system_info = amdgpu_cgs_query_system_info,
1233 .is_virtualization_enabled = amdgpu_cgs_is_virtualization_enabled,
1234 .enter_safe_mode = amdgpu_cgs_enter_safe_mode,
1235 .lock_grbm_idx = amdgpu_cgs_lock_grbm_idx,
1236 .register_pp_handle = amdgpu_cgs_register_pp_handle,
1237 .set_temperature_range = amdgpu_cgs_set_temperature_range,
1240 static const struct cgs_os_ops amdgpu_cgs_os_ops = {
1241 .add_irq_source = amdgpu_cgs_add_irq_source,
1242 .irq_get = amdgpu_cgs_irq_get,
1243 .irq_put = amdgpu_cgs_irq_put
1246 struct cgs_device *amdgpu_cgs_create_device(struct amdgpu_device *adev)
1248 struct amdgpu_cgs_device *cgs_device =
1249 kmalloc(sizeof(*cgs_device), GFP_KERNEL);
1252 DRM_ERROR("Couldn't allocate CGS device structure\n");
1256 cgs_device->base.ops = &amdgpu_cgs_ops;
1257 cgs_device->base.os_ops = &amdgpu_cgs_os_ops;
1258 cgs_device->adev = adev;
1260 return (struct cgs_device *)cgs_device;
1263 void amdgpu_cgs_destroy_device(struct cgs_device *cgs_device)