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 int amdgpu_cgs_alloc_gpu_mem(struct cgs_device *cgs_device,
46 enum cgs_gpu_mem_type type,
47 uint64_t size, uint64_t align,
54 struct amdgpu_bo *obj;
56 /* fail if the alignment is not a power of 2 */
57 if (((align != 1) && (align & (align - 1)))
58 || size == 0 || align == 0)
63 case CGS_GPU_MEM_TYPE__VISIBLE_CONTIG_FB:
64 case CGS_GPU_MEM_TYPE__VISIBLE_FB:
65 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
66 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
67 domain = AMDGPU_GEM_DOMAIN_VRAM;
69 case CGS_GPU_MEM_TYPE__INVISIBLE_CONTIG_FB:
70 case CGS_GPU_MEM_TYPE__INVISIBLE_FB:
71 flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
72 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
73 domain = AMDGPU_GEM_DOMAIN_VRAM;
75 case CGS_GPU_MEM_TYPE__GART_CACHEABLE:
76 domain = AMDGPU_GEM_DOMAIN_GTT;
78 case CGS_GPU_MEM_TYPE__GART_WRITECOMBINE:
79 flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
80 domain = AMDGPU_GEM_DOMAIN_GTT;
89 ret = amdgpu_bo_create(adev, size, align, true, domain, flags,
92 DRM_ERROR("(%d) bo create failed\n", ret);
95 *handle = (cgs_handle_t)obj;
100 static int amdgpu_cgs_free_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle)
102 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
105 int r = amdgpu_bo_reserve(obj, true);
106 if (likely(r == 0)) {
107 amdgpu_bo_kunmap(obj);
108 amdgpu_bo_unpin(obj);
109 amdgpu_bo_unreserve(obj);
111 amdgpu_bo_unref(&obj);
117 static int amdgpu_cgs_gmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle,
121 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
123 WARN_ON_ONCE(obj->placement.num_placement > 1);
125 r = amdgpu_bo_reserve(obj, true);
126 if (unlikely(r != 0))
128 r = amdgpu_bo_pin(obj, obj->preferred_domains, mcaddr);
129 amdgpu_bo_unreserve(obj);
133 static int amdgpu_cgs_gunmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle)
136 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
137 r = amdgpu_bo_reserve(obj, true);
138 if (unlikely(r != 0))
140 r = amdgpu_bo_unpin(obj);
141 amdgpu_bo_unreserve(obj);
145 static int amdgpu_cgs_kmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle,
149 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
150 r = amdgpu_bo_reserve(obj, true);
151 if (unlikely(r != 0))
153 r = amdgpu_bo_kmap(obj, map);
154 amdgpu_bo_unreserve(obj);
158 static int amdgpu_cgs_kunmap_gpu_mem(struct cgs_device *cgs_device, cgs_handle_t handle)
161 struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
162 r = amdgpu_bo_reserve(obj, true);
163 if (unlikely(r != 0))
165 amdgpu_bo_kunmap(obj);
166 amdgpu_bo_unreserve(obj);
170 static uint32_t amdgpu_cgs_read_register(struct cgs_device *cgs_device, unsigned offset)
173 return RREG32(offset);
176 static void amdgpu_cgs_write_register(struct cgs_device *cgs_device, unsigned offset,
180 WREG32(offset, value);
183 static uint32_t amdgpu_cgs_read_ind_register(struct cgs_device *cgs_device,
184 enum cgs_ind_reg space,
189 case CGS_IND_REG__MMIO:
190 return RREG32_IDX(index);
191 case CGS_IND_REG__PCIE:
192 return RREG32_PCIE(index);
193 case CGS_IND_REG__SMC:
194 return RREG32_SMC(index);
195 case CGS_IND_REG__UVD_CTX:
196 return RREG32_UVD_CTX(index);
197 case CGS_IND_REG__DIDT:
198 return RREG32_DIDT(index);
199 case CGS_IND_REG_GC_CAC:
200 return RREG32_GC_CAC(index);
201 case CGS_IND_REG_SE_CAC:
202 return RREG32_SE_CAC(index);
203 case CGS_IND_REG__AUDIO_ENDPT:
204 DRM_ERROR("audio endpt register access not implemented.\n");
207 WARN(1, "Invalid indirect register space");
211 static void amdgpu_cgs_write_ind_register(struct cgs_device *cgs_device,
212 enum cgs_ind_reg space,
213 unsigned index, uint32_t value)
217 case CGS_IND_REG__MMIO:
218 return WREG32_IDX(index, value);
219 case CGS_IND_REG__PCIE:
220 return WREG32_PCIE(index, value);
221 case CGS_IND_REG__SMC:
222 return WREG32_SMC(index, value);
223 case CGS_IND_REG__UVD_CTX:
224 return WREG32_UVD_CTX(index, value);
225 case CGS_IND_REG__DIDT:
226 return WREG32_DIDT(index, value);
227 case CGS_IND_REG_GC_CAC:
228 return WREG32_GC_CAC(index, value);
229 case CGS_IND_REG_SE_CAC:
230 return WREG32_SE_CAC(index, value);
231 case CGS_IND_REG__AUDIO_ENDPT:
232 DRM_ERROR("audio endpt register access not implemented.\n");
235 WARN(1, "Invalid indirect register space");
238 static int amdgpu_cgs_get_pci_resource(struct cgs_device *cgs_device,
239 enum cgs_resource_type resource_type,
242 uint64_t *resource_base)
246 if (resource_base == NULL)
249 switch (resource_type) {
250 case CGS_RESOURCE_TYPE_MMIO:
251 if (adev->rmmio_size == 0)
253 if ((offset + size) > adev->rmmio_size)
255 *resource_base = adev->rmmio_base;
257 case CGS_RESOURCE_TYPE_DOORBELL:
258 if (adev->doorbell.size == 0)
260 if ((offset + size) > adev->doorbell.size)
262 *resource_base = adev->doorbell.base;
264 case CGS_RESOURCE_TYPE_FB:
265 case CGS_RESOURCE_TYPE_IO:
266 case CGS_RESOURCE_TYPE_ROM:
272 static const void *amdgpu_cgs_atom_get_data_table(struct cgs_device *cgs_device,
273 unsigned table, uint16_t *size,
274 uint8_t *frev, uint8_t *crev)
279 if (amdgpu_atom_parse_data_header(
280 adev->mode_info.atom_context, table, size,
281 frev, crev, &data_start))
282 return (uint8_t*)adev->mode_info.atom_context->bios +
288 static int amdgpu_cgs_atom_get_cmd_table_revs(struct cgs_device *cgs_device, unsigned table,
289 uint8_t *frev, uint8_t *crev)
293 if (amdgpu_atom_parse_cmd_header(
294 adev->mode_info.atom_context, table,
301 static int amdgpu_cgs_atom_exec_cmd_table(struct cgs_device *cgs_device, unsigned table,
306 return amdgpu_atom_execute_table(
307 adev->mode_info.atom_context, table, args);
310 struct cgs_irq_params {
312 cgs_irq_source_set_func_t set;
313 cgs_irq_handler_func_t handler;
317 static int cgs_set_irq_state(struct amdgpu_device *adev,
318 struct amdgpu_irq_src *src,
320 enum amdgpu_interrupt_state state)
322 struct cgs_irq_params *irq_params =
323 (struct cgs_irq_params *)src->data;
326 if (!irq_params->set)
328 return irq_params->set(irq_params->private_data,
334 static int cgs_process_irq(struct amdgpu_device *adev,
335 struct amdgpu_irq_src *source,
336 struct amdgpu_iv_entry *entry)
338 struct cgs_irq_params *irq_params =
339 (struct cgs_irq_params *)source->data;
342 if (!irq_params->handler)
344 return irq_params->handler(irq_params->private_data,
349 static const struct amdgpu_irq_src_funcs cgs_irq_funcs = {
350 .set = cgs_set_irq_state,
351 .process = cgs_process_irq,
354 static int amdgpu_cgs_add_irq_source(void *cgs_device,
358 cgs_irq_source_set_func_t set,
359 cgs_irq_handler_func_t handler,
364 struct cgs_irq_params *irq_params;
365 struct amdgpu_irq_src *source =
366 kzalloc(sizeof(struct amdgpu_irq_src), GFP_KERNEL);
370 kzalloc(sizeof(struct cgs_irq_params), GFP_KERNEL);
375 source->num_types = num_types;
376 source->funcs = &cgs_irq_funcs;
377 irq_params->src_id = src_id;
378 irq_params->set = set;
379 irq_params->handler = handler;
380 irq_params->private_data = private_data;
381 source->data = (void *)irq_params;
382 ret = amdgpu_irq_add_id(adev, client_id, src_id, source);
391 static int amdgpu_cgs_irq_get(void *cgs_device, unsigned client_id,
392 unsigned src_id, unsigned type)
396 if (!adev->irq.client[client_id].sources)
399 return amdgpu_irq_get(adev, adev->irq.client[client_id].sources[src_id], type);
402 static int amdgpu_cgs_irq_put(void *cgs_device, unsigned client_id,
403 unsigned src_id, unsigned type)
407 if (!adev->irq.client[client_id].sources)
410 return amdgpu_irq_put(adev, adev->irq.client[client_id].sources[src_id], type);
413 static int amdgpu_cgs_set_clockgating_state(struct cgs_device *cgs_device,
414 enum amd_ip_block_type block_type,
415 enum amd_clockgating_state state)
420 for (i = 0; i < adev->num_ip_blocks; i++) {
421 if (!adev->ip_blocks[i].status.valid)
424 if (adev->ip_blocks[i].version->type == block_type) {
425 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
434 static int amdgpu_cgs_set_powergating_state(struct cgs_device *cgs_device,
435 enum amd_ip_block_type block_type,
436 enum amd_powergating_state state)
441 for (i = 0; i < adev->num_ip_blocks; i++) {
442 if (!adev->ip_blocks[i].status.valid)
445 if (adev->ip_blocks[i].version->type == block_type) {
446 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
456 static uint32_t fw_type_convert(struct cgs_device *cgs_device, uint32_t fw_type)
459 enum AMDGPU_UCODE_ID result = AMDGPU_UCODE_ID_MAXIMUM;
462 case CGS_UCODE_ID_SDMA0:
463 result = AMDGPU_UCODE_ID_SDMA0;
465 case CGS_UCODE_ID_SDMA1:
466 result = AMDGPU_UCODE_ID_SDMA1;
468 case CGS_UCODE_ID_CP_CE:
469 result = AMDGPU_UCODE_ID_CP_CE;
471 case CGS_UCODE_ID_CP_PFP:
472 result = AMDGPU_UCODE_ID_CP_PFP;
474 case CGS_UCODE_ID_CP_ME:
475 result = AMDGPU_UCODE_ID_CP_ME;
477 case CGS_UCODE_ID_CP_MEC:
478 case CGS_UCODE_ID_CP_MEC_JT1:
479 result = AMDGPU_UCODE_ID_CP_MEC1;
481 case CGS_UCODE_ID_CP_MEC_JT2:
482 /* for VI. JT2 should be the same as JT1, because:
483 1, MEC2 and MEC1 use exactly same FW.
484 2, JT2 is not pached but JT1 is.
486 if (adev->asic_type >= CHIP_TOPAZ)
487 result = AMDGPU_UCODE_ID_CP_MEC1;
489 result = AMDGPU_UCODE_ID_CP_MEC2;
491 case CGS_UCODE_ID_RLC_G:
492 result = AMDGPU_UCODE_ID_RLC_G;
494 case CGS_UCODE_ID_STORAGE:
495 result = AMDGPU_UCODE_ID_STORAGE;
498 DRM_ERROR("Firmware type not supported\n");
503 static int amdgpu_cgs_rel_firmware(struct cgs_device *cgs_device, enum cgs_ucode_id type)
506 if ((CGS_UCODE_ID_SMU == type) || (CGS_UCODE_ID_SMU_SK == type)) {
507 release_firmware(adev->pm.fw);
511 /* cannot release other firmware because they are not created by cgs */
515 static uint16_t amdgpu_get_firmware_version(struct cgs_device *cgs_device,
516 enum cgs_ucode_id type)
519 uint16_t fw_version = 0;
522 case CGS_UCODE_ID_SDMA0:
523 fw_version = adev->sdma.instance[0].fw_version;
525 case CGS_UCODE_ID_SDMA1:
526 fw_version = adev->sdma.instance[1].fw_version;
528 case CGS_UCODE_ID_CP_CE:
529 fw_version = adev->gfx.ce_fw_version;
531 case CGS_UCODE_ID_CP_PFP:
532 fw_version = adev->gfx.pfp_fw_version;
534 case CGS_UCODE_ID_CP_ME:
535 fw_version = adev->gfx.me_fw_version;
537 case CGS_UCODE_ID_CP_MEC:
538 fw_version = adev->gfx.mec_fw_version;
540 case CGS_UCODE_ID_CP_MEC_JT1:
541 fw_version = adev->gfx.mec_fw_version;
543 case CGS_UCODE_ID_CP_MEC_JT2:
544 fw_version = adev->gfx.mec_fw_version;
546 case CGS_UCODE_ID_RLC_G:
547 fw_version = adev->gfx.rlc_fw_version;
549 case CGS_UCODE_ID_STORAGE:
552 DRM_ERROR("firmware type %d do not have version\n", type);
558 static int amdgpu_cgs_enter_safe_mode(struct cgs_device *cgs_device,
563 if (adev->gfx.rlc.funcs->enter_safe_mode == NULL ||
564 adev->gfx.rlc.funcs->exit_safe_mode == NULL)
568 adev->gfx.rlc.funcs->enter_safe_mode(adev);
570 adev->gfx.rlc.funcs->exit_safe_mode(adev);
575 static void amdgpu_cgs_lock_grbm_idx(struct cgs_device *cgs_device,
581 mutex_lock(&adev->grbm_idx_mutex);
583 mutex_unlock(&adev->grbm_idx_mutex);
586 static int amdgpu_cgs_get_firmware_info(struct cgs_device *cgs_device,
587 enum cgs_ucode_id type,
588 struct cgs_firmware_info *info)
592 if ((CGS_UCODE_ID_SMU != type) && (CGS_UCODE_ID_SMU_SK != type)) {
595 const struct gfx_firmware_header_v1_0 *header;
596 enum AMDGPU_UCODE_ID id;
597 struct amdgpu_firmware_info *ucode;
599 id = fw_type_convert(cgs_device, type);
600 ucode = &adev->firmware.ucode[id];
601 if (ucode->fw == NULL)
604 gpu_addr = ucode->mc_addr;
605 header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
606 data_size = le32_to_cpu(header->header.ucode_size_bytes);
608 if ((type == CGS_UCODE_ID_CP_MEC_JT1) ||
609 (type == CGS_UCODE_ID_CP_MEC_JT2)) {
610 gpu_addr += ALIGN(le32_to_cpu(header->header.ucode_size_bytes), PAGE_SIZE);
611 data_size = le32_to_cpu(header->jt_size) << 2;
614 info->kptr = ucode->kaddr;
615 info->image_size = data_size;
616 info->mc_addr = gpu_addr;
617 info->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
619 if (CGS_UCODE_ID_CP_MEC == type)
620 info->image_size = le32_to_cpu(header->jt_offset) << 2;
622 info->fw_version = amdgpu_get_firmware_version(cgs_device, type);
623 info->feature_version = (uint16_t)le32_to_cpu(header->ucode_feature_version);
625 char fw_name[30] = {0};
628 uint32_t ucode_start_address;
630 const struct smc_firmware_header_v1_0 *hdr;
631 const struct common_firmware_header *header;
632 struct amdgpu_firmware_info *ucode = NULL;
635 switch (adev->asic_type) {
637 strcpy(fw_name, "radeon/tahiti_smc.bin");
640 if ((adev->pdev->revision == 0x81) &&
641 ((adev->pdev->device == 0x6810) ||
642 (adev->pdev->device == 0x6811))) {
643 info->is_kicker = true;
644 strcpy(fw_name, "radeon/pitcairn_k_smc.bin");
646 strcpy(fw_name, "radeon/pitcairn_smc.bin");
650 if (((adev->pdev->device == 0x6820) &&
651 ((adev->pdev->revision == 0x81) ||
652 (adev->pdev->revision == 0x83))) ||
653 ((adev->pdev->device == 0x6821) &&
654 ((adev->pdev->revision == 0x83) ||
655 (adev->pdev->revision == 0x87))) ||
656 ((adev->pdev->revision == 0x87) &&
657 ((adev->pdev->device == 0x6823) ||
658 (adev->pdev->device == 0x682b)))) {
659 info->is_kicker = true;
660 strcpy(fw_name, "radeon/verde_k_smc.bin");
662 strcpy(fw_name, "radeon/verde_smc.bin");
666 if (((adev->pdev->revision == 0x81) &&
667 ((adev->pdev->device == 0x6600) ||
668 (adev->pdev->device == 0x6604) ||
669 (adev->pdev->device == 0x6605) ||
670 (adev->pdev->device == 0x6610))) ||
671 ((adev->pdev->revision == 0x83) &&
672 (adev->pdev->device == 0x6610))) {
673 info->is_kicker = true;
674 strcpy(fw_name, "radeon/oland_k_smc.bin");
676 strcpy(fw_name, "radeon/oland_smc.bin");
680 if (((adev->pdev->revision == 0x81) &&
681 (adev->pdev->device == 0x6660)) ||
682 ((adev->pdev->revision == 0x83) &&
683 ((adev->pdev->device == 0x6660) ||
684 (adev->pdev->device == 0x6663) ||
685 (adev->pdev->device == 0x6665) ||
686 (adev->pdev->device == 0x6667)))) {
687 info->is_kicker = true;
688 strcpy(fw_name, "radeon/hainan_k_smc.bin");
689 } else if ((adev->pdev->revision == 0xc3) &&
690 (adev->pdev->device == 0x6665)) {
691 info->is_kicker = true;
692 strcpy(fw_name, "radeon/banks_k_2_smc.bin");
694 strcpy(fw_name, "radeon/hainan_smc.bin");
698 if ((adev->pdev->revision == 0x80) ||
699 (adev->pdev->revision == 0x81) ||
700 (adev->pdev->device == 0x665f)) {
701 info->is_kicker = true;
702 strcpy(fw_name, "radeon/bonaire_k_smc.bin");
704 strcpy(fw_name, "radeon/bonaire_smc.bin");
708 if (adev->pdev->revision == 0x80) {
709 info->is_kicker = true;
710 strcpy(fw_name, "radeon/hawaii_k_smc.bin");
712 strcpy(fw_name, "radeon/hawaii_smc.bin");
716 if (((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x81)) ||
717 ((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x83)) ||
718 ((adev->pdev->device == 0x6907) && (adev->pdev->revision == 0x87))) {
719 info->is_kicker = true;
720 strcpy(fw_name, "amdgpu/topaz_k_smc.bin");
722 strcpy(fw_name, "amdgpu/topaz_smc.bin");
725 if (((adev->pdev->device == 0x6939) && (adev->pdev->revision == 0xf1)) ||
726 ((adev->pdev->device == 0x6938) && (adev->pdev->revision == 0xf1))) {
727 info->is_kicker = true;
728 strcpy(fw_name, "amdgpu/tonga_k_smc.bin");
730 strcpy(fw_name, "amdgpu/tonga_smc.bin");
733 strcpy(fw_name, "amdgpu/fiji_smc.bin");
736 if (type == CGS_UCODE_ID_SMU) {
737 if (((adev->pdev->device == 0x67ef) &&
738 ((adev->pdev->revision == 0xe0) ||
739 (adev->pdev->revision == 0xe2) ||
740 (adev->pdev->revision == 0xe5))) ||
741 ((adev->pdev->device == 0x67ff) &&
742 ((adev->pdev->revision == 0xcf) ||
743 (adev->pdev->revision == 0xef) ||
744 (adev->pdev->revision == 0xff)))) {
745 info->is_kicker = true;
746 strcpy(fw_name, "amdgpu/polaris11_k_smc.bin");
748 strcpy(fw_name, "amdgpu/polaris11_smc.bin");
749 } else if (type == CGS_UCODE_ID_SMU_SK) {
750 strcpy(fw_name, "amdgpu/polaris11_smc_sk.bin");
754 if (type == CGS_UCODE_ID_SMU) {
755 if ((adev->pdev->device == 0x67df) &&
756 ((adev->pdev->revision == 0xe0) ||
757 (adev->pdev->revision == 0xe3) ||
758 (adev->pdev->revision == 0xe4) ||
759 (adev->pdev->revision == 0xe5) ||
760 (adev->pdev->revision == 0xe7) ||
761 (adev->pdev->revision == 0xef))) {
762 info->is_kicker = true;
763 strcpy(fw_name, "amdgpu/polaris10_k_smc.bin");
765 strcpy(fw_name, "amdgpu/polaris10_smc.bin");
766 } else if (type == CGS_UCODE_ID_SMU_SK) {
767 strcpy(fw_name, "amdgpu/polaris10_smc_sk.bin");
771 strcpy(fw_name, "amdgpu/polaris12_smc.bin");
774 if ((adev->pdev->device == 0x687f) &&
775 ((adev->pdev->revision == 0xc0) ||
776 (adev->pdev->revision == 0xc1) ||
777 (adev->pdev->revision == 0xc3)))
778 strcpy(fw_name, "amdgpu/vega10_acg_smc.bin");
780 strcpy(fw_name, "amdgpu/vega10_smc.bin");
783 DRM_ERROR("SMC firmware not supported\n");
787 err = request_firmware(&adev->pm.fw, fw_name, adev->dev);
789 DRM_ERROR("Failed to request firmware\n");
793 err = amdgpu_ucode_validate(adev->pm.fw);
795 DRM_ERROR("Failed to load firmware \"%s\"", fw_name);
796 release_firmware(adev->pm.fw);
801 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
802 ucode = &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
803 ucode->ucode_id = AMDGPU_UCODE_ID_SMC;
804 ucode->fw = adev->pm.fw;
805 header = (const struct common_firmware_header *)ucode->fw->data;
806 adev->firmware.fw_size +=
807 ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
811 hdr = (const struct smc_firmware_header_v1_0 *) adev->pm.fw->data;
812 amdgpu_ucode_print_smc_hdr(&hdr->header);
813 adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
814 ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
815 ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
816 src = (const uint8_t *)(adev->pm.fw->data +
817 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
819 info->version = adev->pm.fw_version;
820 info->image_size = ucode_size;
821 info->ucode_start_address = ucode_start_address;
822 info->kptr = (void *)src;
827 static int amdgpu_cgs_is_virtualization_enabled(void *cgs_device)
830 return amdgpu_sriov_vf(adev);
833 static int amdgpu_cgs_query_system_info(struct cgs_device *cgs_device,
834 struct cgs_system_info *sys_info)
838 if (NULL == sys_info)
841 if (sizeof(struct cgs_system_info) != sys_info->size)
844 switch (sys_info->info_id) {
845 case CGS_SYSTEM_INFO_ADAPTER_BDF_ID:
846 sys_info->value = adev->pdev->devfn | (adev->pdev->bus->number << 8);
848 case CGS_SYSTEM_INFO_PCIE_GEN_INFO:
849 sys_info->value = adev->pm.pcie_gen_mask;
851 case CGS_SYSTEM_INFO_PCIE_MLW:
852 sys_info->value = adev->pm.pcie_mlw_mask;
854 case CGS_SYSTEM_INFO_PCIE_DEV:
855 sys_info->value = adev->pdev->device;
857 case CGS_SYSTEM_INFO_PCIE_REV:
858 sys_info->value = adev->pdev->revision;
860 case CGS_SYSTEM_INFO_CG_FLAGS:
861 sys_info->value = adev->cg_flags;
863 case CGS_SYSTEM_INFO_PG_FLAGS:
864 sys_info->value = adev->pg_flags;
866 case CGS_SYSTEM_INFO_GFX_CU_INFO:
867 sys_info->value = adev->gfx.cu_info.number;
869 case CGS_SYSTEM_INFO_GFX_SE_INFO:
870 sys_info->value = adev->gfx.config.max_shader_engines;
872 case CGS_SYSTEM_INFO_PCIE_SUB_SYS_ID:
873 sys_info->value = adev->pdev->subsystem_device;
875 case CGS_SYSTEM_INFO_PCIE_SUB_SYS_VENDOR_ID:
876 sys_info->value = adev->pdev->subsystem_vendor;
878 case CGS_SYSTEM_INFO_PCIE_BUS_DEVFN:
879 sys_info->value = adev->pdev->devfn;
888 static int amdgpu_cgs_get_active_displays_info(struct cgs_device *cgs_device,
889 struct cgs_display_info *info)
892 struct amdgpu_crtc *amdgpu_crtc;
893 struct drm_device *ddev = adev->ddev;
894 struct drm_crtc *crtc;
895 uint32_t line_time_us, vblank_lines;
896 struct cgs_mode_info *mode_info;
901 mode_info = info->mode_info;
903 /* if the displays are off, vblank time is max */
904 mode_info->vblank_time_us = 0xffffffff;
905 /* always set the reference clock */
906 mode_info->ref_clock = adev->clock.spll.reference_freq;
909 if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
910 list_for_each_entry(crtc,
911 &ddev->mode_config.crtc_list, head) {
912 amdgpu_crtc = to_amdgpu_crtc(crtc);
914 info->active_display_mask |= (1 << amdgpu_crtc->crtc_id);
915 info->display_count++;
917 if (mode_info != NULL &&
918 crtc->enabled && amdgpu_crtc->enabled &&
919 amdgpu_crtc->hw_mode.clock) {
920 line_time_us = (amdgpu_crtc->hw_mode.crtc_htotal * 1000) /
921 amdgpu_crtc->hw_mode.clock;
922 vblank_lines = amdgpu_crtc->hw_mode.crtc_vblank_end -
923 amdgpu_crtc->hw_mode.crtc_vdisplay +
924 (amdgpu_crtc->v_border * 2);
925 mode_info->vblank_time_us = vblank_lines * line_time_us;
926 mode_info->refresh_rate = drm_mode_vrefresh(&amdgpu_crtc->hw_mode);
927 mode_info->ref_clock = adev->clock.spll.reference_freq;
937 static int amdgpu_cgs_notify_dpm_enabled(struct cgs_device *cgs_device, bool enabled)
941 adev->pm.dpm_enabled = enabled;
946 /** \brief evaluate acpi namespace object, handle or pathname must be valid
948 * \param info input/output arguments for the control method
952 #if defined(CONFIG_ACPI)
953 static int amdgpu_cgs_acpi_eval_object(struct cgs_device *cgs_device,
954 struct cgs_acpi_method_info *info)
958 struct acpi_object_list input;
959 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
960 union acpi_object *params, *obj;
961 uint8_t name[5] = {'\0'};
962 struct cgs_acpi_method_argument *argument;
967 handle = ACPI_HANDLE(&adev->pdev->dev);
971 memset(&input, 0, sizeof(struct acpi_object_list));
973 /* validate input info */
974 if (info->size != sizeof(struct cgs_acpi_method_info))
977 input.count = info->input_count;
978 if (info->input_count > 0) {
979 if (info->pinput_argument == NULL)
981 argument = info->pinput_argument;
982 for (i = 0; i < info->input_count; i++) {
983 if (((argument->type == ACPI_TYPE_STRING) ||
984 (argument->type == ACPI_TYPE_BUFFER)) &&
985 (argument->pointer == NULL))
991 if (info->output_count > 0) {
992 if (info->poutput_argument == NULL)
994 argument = info->poutput_argument;
995 for (i = 0; i < info->output_count; i++) {
996 if (((argument->type == ACPI_TYPE_STRING) ||
997 (argument->type == ACPI_TYPE_BUFFER))
998 && (argument->pointer == NULL))
1004 /* The path name passed to acpi_evaluate_object should be null terminated */
1005 if ((info->field & CGS_ACPI_FIELD_METHOD_NAME) != 0) {
1006 strncpy(name, (char *)&(info->name), sizeof(uint32_t));
1010 /* parse input parameters */
1011 if (input.count > 0) {
1012 input.pointer = params =
1013 kzalloc(sizeof(union acpi_object) * input.count, GFP_KERNEL);
1017 argument = info->pinput_argument;
1019 for (i = 0; i < input.count; i++) {
1020 params->type = argument->type;
1021 switch (params->type) {
1022 case ACPI_TYPE_INTEGER:
1023 params->integer.value = argument->value;
1025 case ACPI_TYPE_STRING:
1026 params->string.length = argument->data_length;
1027 params->string.pointer = argument->pointer;
1029 case ACPI_TYPE_BUFFER:
1030 params->buffer.length = argument->data_length;
1031 params->buffer.pointer = argument->pointer;
1041 /* parse output info */
1042 count = info->output_count;
1043 argument = info->poutput_argument;
1045 /* evaluate the acpi method */
1046 status = acpi_evaluate_object(handle, name, &input, &output);
1048 if (ACPI_FAILURE(status)) {
1053 /* return the output info */
1054 obj = output.pointer;
1057 if ((obj->type != ACPI_TYPE_PACKAGE) ||
1058 (obj->package.count != count)) {
1062 params = obj->package.elements;
1066 if (params == NULL) {
1071 for (i = 0; i < count; i++) {
1072 if (argument->type != params->type) {
1076 switch (params->type) {
1077 case ACPI_TYPE_INTEGER:
1078 argument->value = params->integer.value;
1080 case ACPI_TYPE_STRING:
1081 if ((params->string.length != argument->data_length) ||
1082 (params->string.pointer == NULL)) {
1086 strncpy(argument->pointer,
1087 params->string.pointer,
1088 params->string.length);
1090 case ACPI_TYPE_BUFFER:
1091 if (params->buffer.pointer == NULL) {
1095 memcpy(argument->pointer,
1096 params->buffer.pointer,
1097 argument->data_length);
1110 kfree((void *)input.pointer);
1114 static int amdgpu_cgs_acpi_eval_object(struct cgs_device *cgs_device,
1115 struct cgs_acpi_method_info *info)
1121 static int amdgpu_cgs_call_acpi_method(struct cgs_device *cgs_device,
1122 uint32_t acpi_method,
1123 uint32_t acpi_function,
1124 void *pinput, void *poutput,
1125 uint32_t output_count,
1126 uint32_t input_size,
1127 uint32_t output_size)
1129 struct cgs_acpi_method_argument acpi_input[2] = { {0}, {0} };
1130 struct cgs_acpi_method_argument acpi_output = {0};
1131 struct cgs_acpi_method_info info = {0};
1133 acpi_input[0].type = CGS_ACPI_TYPE_INTEGER;
1134 acpi_input[0].data_length = sizeof(uint32_t);
1135 acpi_input[0].value = acpi_function;
1137 acpi_input[1].type = CGS_ACPI_TYPE_BUFFER;
1138 acpi_input[1].data_length = input_size;
1139 acpi_input[1].pointer = pinput;
1141 acpi_output.type = CGS_ACPI_TYPE_BUFFER;
1142 acpi_output.data_length = output_size;
1143 acpi_output.pointer = poutput;
1145 info.size = sizeof(struct cgs_acpi_method_info);
1146 info.field = CGS_ACPI_FIELD_METHOD_NAME | CGS_ACPI_FIELD_INPUT_ARGUMENT_COUNT;
1147 info.input_count = 2;
1148 info.name = acpi_method;
1149 info.pinput_argument = acpi_input;
1150 info.output_count = output_count;
1151 info.poutput_argument = &acpi_output;
1153 return amdgpu_cgs_acpi_eval_object(cgs_device, &info);
1156 static const struct cgs_ops amdgpu_cgs_ops = {
1157 .alloc_gpu_mem = amdgpu_cgs_alloc_gpu_mem,
1158 .free_gpu_mem = amdgpu_cgs_free_gpu_mem,
1159 .gmap_gpu_mem = amdgpu_cgs_gmap_gpu_mem,
1160 .gunmap_gpu_mem = amdgpu_cgs_gunmap_gpu_mem,
1161 .kmap_gpu_mem = amdgpu_cgs_kmap_gpu_mem,
1162 .kunmap_gpu_mem = amdgpu_cgs_kunmap_gpu_mem,
1163 .read_register = amdgpu_cgs_read_register,
1164 .write_register = amdgpu_cgs_write_register,
1165 .read_ind_register = amdgpu_cgs_read_ind_register,
1166 .write_ind_register = amdgpu_cgs_write_ind_register,
1167 .get_pci_resource = amdgpu_cgs_get_pci_resource,
1168 .atom_get_data_table = amdgpu_cgs_atom_get_data_table,
1169 .atom_get_cmd_table_revs = amdgpu_cgs_atom_get_cmd_table_revs,
1170 .atom_exec_cmd_table = amdgpu_cgs_atom_exec_cmd_table,
1171 .get_firmware_info = amdgpu_cgs_get_firmware_info,
1172 .rel_firmware = amdgpu_cgs_rel_firmware,
1173 .set_powergating_state = amdgpu_cgs_set_powergating_state,
1174 .set_clockgating_state = amdgpu_cgs_set_clockgating_state,
1175 .get_active_displays_info = amdgpu_cgs_get_active_displays_info,
1176 .notify_dpm_enabled = amdgpu_cgs_notify_dpm_enabled,
1177 .call_acpi_method = amdgpu_cgs_call_acpi_method,
1178 .query_system_info = amdgpu_cgs_query_system_info,
1179 .is_virtualization_enabled = amdgpu_cgs_is_virtualization_enabled,
1180 .enter_safe_mode = amdgpu_cgs_enter_safe_mode,
1181 .lock_grbm_idx = amdgpu_cgs_lock_grbm_idx,
1184 static const struct cgs_os_ops amdgpu_cgs_os_ops = {
1185 .add_irq_source = amdgpu_cgs_add_irq_source,
1186 .irq_get = amdgpu_cgs_irq_get,
1187 .irq_put = amdgpu_cgs_irq_put
1190 struct cgs_device *amdgpu_cgs_create_device(struct amdgpu_device *adev)
1192 struct amdgpu_cgs_device *cgs_device =
1193 kmalloc(sizeof(*cgs_device), GFP_KERNEL);
1196 DRM_ERROR("Couldn't allocate CGS device structure\n");
1200 cgs_device->base.ops = &amdgpu_cgs_ops;
1201 cgs_device->base.os_ops = &amdgpu_cgs_os_ops;
1202 cgs_device->adev = adev;
1204 return (struct cgs_device *)cgs_device;
1207 void amdgpu_cgs_destroy_device(struct cgs_device *cgs_device)