fbmem: don't allow too huge resolutions
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_atomfirmware.c
1 /*
2  * Copyright 2016 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23
24 #include <drm/amdgpu_drm.h>
25 #include "amdgpu.h"
26 #include "atomfirmware.h"
27 #include "amdgpu_atomfirmware.h"
28 #include "atom.h"
29 #include "atombios.h"
30 #include "soc15_hw_ip.h"
31
32 union firmware_info {
33         struct atom_firmware_info_v3_1 v31;
34         struct atom_firmware_info_v3_2 v32;
35         struct atom_firmware_info_v3_3 v33;
36         struct atom_firmware_info_v3_4 v34;
37 };
38
39 /*
40  * Helper function to query firmware capability
41  *
42  * @adev: amdgpu_device pointer
43  *
44  * Return firmware_capability in firmwareinfo table on success or 0 if not
45  */
46 uint32_t amdgpu_atomfirmware_query_firmware_capability(struct amdgpu_device *adev)
47 {
48         struct amdgpu_mode_info *mode_info = &adev->mode_info;
49         int index;
50         u16 data_offset, size;
51         union firmware_info *firmware_info;
52         u8 frev, crev;
53         u32 fw_cap = 0;
54
55         index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
56                         firmwareinfo);
57
58         if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context,
59                                 index, &size, &frev, &crev, &data_offset)) {
60                 /* support firmware_info 3.1 + */
61                 if ((frev == 3 && crev >=1) || (frev > 3)) {
62                         firmware_info = (union firmware_info *)
63                                 (mode_info->atom_context->bios + data_offset);
64                         fw_cap = le32_to_cpu(firmware_info->v31.firmware_capability);
65                 }
66         }
67
68         return fw_cap;
69 }
70
71 /*
72  * Helper function to query gpu virtualizaiton capability
73  *
74  * @adev: amdgpu_device pointer
75  *
76  * Return true if gpu virtualization is supported or false if not
77  */
78 bool amdgpu_atomfirmware_gpu_virtualization_supported(struct amdgpu_device *adev)
79 {
80         u32 fw_cap;
81
82         fw_cap = adev->mode_info.firmware_flags;
83
84         return (fw_cap & ATOM_FIRMWARE_CAP_GPU_VIRTUALIZATION) ? true : false;
85 }
86
87 void amdgpu_atomfirmware_scratch_regs_init(struct amdgpu_device *adev)
88 {
89         int index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
90                                                 firmwareinfo);
91         uint16_t data_offset;
92
93         if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, NULL,
94                                           NULL, NULL, &data_offset)) {
95                 struct atom_firmware_info_v3_1 *firmware_info =
96                         (struct atom_firmware_info_v3_1 *)(adev->mode_info.atom_context->bios +
97                                                            data_offset);
98
99                 adev->bios_scratch_reg_offset =
100                         le32_to_cpu(firmware_info->bios_scratch_reg_startaddr);
101         }
102 }
103
104 int amdgpu_atomfirmware_allocate_fb_scratch(struct amdgpu_device *adev)
105 {
106         struct atom_context *ctx = adev->mode_info.atom_context;
107         int index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
108                                                 vram_usagebyfirmware);
109         struct vram_usagebyfirmware_v2_1 *firmware_usage;
110         uint32_t start_addr, size;
111         uint16_t data_offset;
112         int usage_bytes = 0;
113
114         if (amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
115                 firmware_usage = (struct vram_usagebyfirmware_v2_1 *)(ctx->bios + data_offset);
116                 DRM_DEBUG("atom firmware requested %08x %dkb fw %dkb drv\n",
117                           le32_to_cpu(firmware_usage->start_address_in_kb),
118                           le16_to_cpu(firmware_usage->used_by_firmware_in_kb),
119                           le16_to_cpu(firmware_usage->used_by_driver_in_kb));
120
121                 start_addr = le32_to_cpu(firmware_usage->start_address_in_kb);
122                 size = le16_to_cpu(firmware_usage->used_by_firmware_in_kb);
123
124                 if ((uint32_t)(start_addr & ATOM_VRAM_OPERATION_FLAGS_MASK) ==
125                         (uint32_t)(ATOM_VRAM_BLOCK_SRIOV_MSG_SHARE_RESERVATION <<
126                         ATOM_VRAM_OPERATION_FLAGS_SHIFT)) {
127                         /* Firmware request VRAM reservation for SR-IOV */
128                         adev->mman.fw_vram_usage_start_offset = (start_addr &
129                                 (~ATOM_VRAM_OPERATION_FLAGS_MASK)) << 10;
130                         adev->mman.fw_vram_usage_size = size << 10;
131                         /* Use the default scratch size */
132                         usage_bytes = 0;
133                 } else {
134                         usage_bytes = le16_to_cpu(firmware_usage->used_by_driver_in_kb) << 10;
135                 }
136         }
137         ctx->scratch_size_bytes = 0;
138         if (usage_bytes == 0)
139                 usage_bytes = 20 * 1024;
140         /* allocate some scratch memory */
141         ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);
142         if (!ctx->scratch)
143                 return -ENOMEM;
144         ctx->scratch_size_bytes = usage_bytes;
145         return 0;
146 }
147
148 union igp_info {
149         struct atom_integrated_system_info_v1_11 v11;
150         struct atom_integrated_system_info_v1_12 v12;
151         struct atom_integrated_system_info_v2_1 v21;
152 };
153
154 union umc_info {
155         struct atom_umc_info_v3_1 v31;
156         struct atom_umc_info_v3_2 v32;
157         struct atom_umc_info_v3_3 v33;
158 };
159
160 union vram_info {
161         struct atom_vram_info_header_v2_3 v23;
162         struct atom_vram_info_header_v2_4 v24;
163         struct atom_vram_info_header_v2_5 v25;
164         struct atom_vram_info_header_v2_6 v26;
165 };
166
167 union vram_module {
168         struct atom_vram_module_v9 v9;
169         struct atom_vram_module_v10 v10;
170         struct atom_vram_module_v11 v11;
171 };
172
173 static int convert_atom_mem_type_to_vram_type(struct amdgpu_device *adev,
174                                               int atom_mem_type)
175 {
176         int vram_type;
177
178         if (adev->flags & AMD_IS_APU) {
179                 switch (atom_mem_type) {
180                 case Ddr2MemType:
181                 case LpDdr2MemType:
182                         vram_type = AMDGPU_VRAM_TYPE_DDR2;
183                         break;
184                 case Ddr3MemType:
185                 case LpDdr3MemType:
186                         vram_type = AMDGPU_VRAM_TYPE_DDR3;
187                         break;
188                 case Ddr4MemType:
189                 case LpDdr4MemType:
190                         vram_type = AMDGPU_VRAM_TYPE_DDR4;
191                         break;
192                 case Ddr5MemType:
193                 case LpDdr5MemType:
194                         vram_type = AMDGPU_VRAM_TYPE_DDR5;
195                         break;
196                 default:
197                         vram_type = AMDGPU_VRAM_TYPE_UNKNOWN;
198                         break;
199                 }
200         } else {
201                 switch (atom_mem_type) {
202                 case ATOM_DGPU_VRAM_TYPE_GDDR5:
203                         vram_type = AMDGPU_VRAM_TYPE_GDDR5;
204                         break;
205                 case ATOM_DGPU_VRAM_TYPE_HBM2:
206                 case ATOM_DGPU_VRAM_TYPE_HBM2E:
207                         vram_type = AMDGPU_VRAM_TYPE_HBM;
208                         break;
209                 case ATOM_DGPU_VRAM_TYPE_GDDR6:
210                         vram_type = AMDGPU_VRAM_TYPE_GDDR6;
211                         break;
212                 default:
213                         vram_type = AMDGPU_VRAM_TYPE_UNKNOWN;
214                         break;
215                 }
216         }
217
218         return vram_type;
219 }
220
221
222 int
223 amdgpu_atomfirmware_get_vram_info(struct amdgpu_device *adev,
224                                   int *vram_width, int *vram_type,
225                                   int *vram_vendor)
226 {
227         struct amdgpu_mode_info *mode_info = &adev->mode_info;
228         int index, i = 0;
229         u16 data_offset, size;
230         union igp_info *igp_info;
231         union vram_info *vram_info;
232         union vram_module *vram_module;
233         u8 frev, crev;
234         u8 mem_type;
235         u8 mem_vendor;
236         u32 mem_channel_number;
237         u32 mem_channel_width;
238         u32 module_id;
239
240         if (adev->flags & AMD_IS_APU)
241                 index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
242                                                     integratedsysteminfo);
243         else
244                 index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
245                                                     vram_info);
246
247         if (amdgpu_atom_parse_data_header(mode_info->atom_context,
248                                           index, &size,
249                                           &frev, &crev, &data_offset)) {
250                 if (adev->flags & AMD_IS_APU) {
251                         igp_info = (union igp_info *)
252                                 (mode_info->atom_context->bios + data_offset);
253                         switch (frev) {
254                         case 1:
255                                 switch (crev) {
256                                 case 11:
257                                 case 12:
258                                         mem_channel_number = igp_info->v11.umachannelnumber;
259                                         if (!mem_channel_number)
260                                                 mem_channel_number = 1;
261                                         /* channel width is 64 */
262                                         if (vram_width)
263                                                 *vram_width = mem_channel_number * 64;
264                                         mem_type = igp_info->v11.memorytype;
265                                         if (vram_type)
266                                                 *vram_type = convert_atom_mem_type_to_vram_type(adev, mem_type);
267                                         break;
268                                 default:
269                                         return -EINVAL;
270                                 }
271                                 break;
272                         case 2:
273                                 switch (crev) {
274                                 case 1:
275                                 case 2:
276                                         mem_channel_number = igp_info->v21.umachannelnumber;
277                                         if (!mem_channel_number)
278                                                 mem_channel_number = 1;
279                                         /* channel width is 64 */
280                                         if (vram_width)
281                                                 *vram_width = mem_channel_number * 64;
282                                         mem_type = igp_info->v21.memorytype;
283                                         if (vram_type)
284                                                 *vram_type = convert_atom_mem_type_to_vram_type(adev, mem_type);
285                                         break;
286                                 default:
287                                         return -EINVAL;
288                                 }
289                                 break;
290                         default:
291                                 return -EINVAL;
292                         }
293                 } else {
294                         vram_info = (union vram_info *)
295                                 (mode_info->atom_context->bios + data_offset);
296                         module_id = (RREG32(adev->bios_scratch_reg_offset + 4) & 0x00ff0000) >> 16;
297                         switch (crev) {
298                         case 3:
299                                 if (module_id > vram_info->v23.vram_module_num)
300                                         module_id = 0;
301                                 vram_module = (union vram_module *)vram_info->v23.vram_module;
302                                 while (i < module_id) {
303                                         vram_module = (union vram_module *)
304                                                 ((u8 *)vram_module + vram_module->v9.vram_module_size);
305                                         i++;
306                                 }
307                                 mem_type = vram_module->v9.memory_type;
308                                 if (vram_type)
309                                         *vram_type = convert_atom_mem_type_to_vram_type(adev, mem_type);
310                                 mem_channel_number = vram_module->v9.channel_num;
311                                 mem_channel_width = vram_module->v9.channel_width;
312                                 if (vram_width)
313                                         *vram_width = mem_channel_number * (1 << mem_channel_width);
314                                 mem_vendor = (vram_module->v9.vender_rev_id) & 0xF;
315                                 if (vram_vendor)
316                                         *vram_vendor = mem_vendor;
317                                 break;
318                         case 4:
319                                 if (module_id > vram_info->v24.vram_module_num)
320                                         module_id = 0;
321                                 vram_module = (union vram_module *)vram_info->v24.vram_module;
322                                 while (i < module_id) {
323                                         vram_module = (union vram_module *)
324                                                 ((u8 *)vram_module + vram_module->v10.vram_module_size);
325                                         i++;
326                                 }
327                                 mem_type = vram_module->v10.memory_type;
328                                 if (vram_type)
329                                         *vram_type = convert_atom_mem_type_to_vram_type(adev, mem_type);
330                                 mem_channel_number = vram_module->v10.channel_num;
331                                 mem_channel_width = vram_module->v10.channel_width;
332                                 if (vram_width)
333                                         *vram_width = mem_channel_number * (1 << mem_channel_width);
334                                 mem_vendor = (vram_module->v10.vender_rev_id) & 0xF;
335                                 if (vram_vendor)
336                                         *vram_vendor = mem_vendor;
337                                 break;
338                         case 5:
339                                 if (module_id > vram_info->v25.vram_module_num)
340                                         module_id = 0;
341                                 vram_module = (union vram_module *)vram_info->v25.vram_module;
342                                 while (i < module_id) {
343                                         vram_module = (union vram_module *)
344                                                 ((u8 *)vram_module + vram_module->v11.vram_module_size);
345                                         i++;
346                                 }
347                                 mem_type = vram_module->v11.memory_type;
348                                 if (vram_type)
349                                         *vram_type = convert_atom_mem_type_to_vram_type(adev, mem_type);
350                                 mem_channel_number = vram_module->v11.channel_num;
351                                 mem_channel_width = vram_module->v11.channel_width;
352                                 if (vram_width)
353                                         *vram_width = mem_channel_number * (1 << mem_channel_width);
354                                 mem_vendor = (vram_module->v11.vender_rev_id) & 0xF;
355                                 if (vram_vendor)
356                                         *vram_vendor = mem_vendor;
357                                 break;
358                         case 6:
359                                 if (module_id > vram_info->v26.vram_module_num)
360                                         module_id = 0;
361                                 vram_module = (union vram_module *)vram_info->v26.vram_module;
362                                 while (i < module_id) {
363                                         vram_module = (union vram_module *)
364                                                 ((u8 *)vram_module + vram_module->v9.vram_module_size);
365                                         i++;
366                                 }
367                                 mem_type = vram_module->v9.memory_type;
368                                 if (vram_type)
369                                         *vram_type = convert_atom_mem_type_to_vram_type(adev, mem_type);
370                                 mem_channel_number = vram_module->v9.channel_num;
371                                 mem_channel_width = vram_module->v9.channel_width;
372                                 if (vram_width)
373                                         *vram_width = mem_channel_number * (1 << mem_channel_width);
374                                 mem_vendor = (vram_module->v9.vender_rev_id) & 0xF;
375                                 if (vram_vendor)
376                                         *vram_vendor = mem_vendor;
377                                 break;
378                         default:
379                                 return -EINVAL;
380                         }
381                 }
382
383         }
384
385         return 0;
386 }
387
388 /*
389  * Return true if vbios enabled ecc by default, if umc info table is available
390  * or false if ecc is not enabled or umc info table is not available
391  */
392 bool amdgpu_atomfirmware_mem_ecc_supported(struct amdgpu_device *adev)
393 {
394         struct amdgpu_mode_info *mode_info = &adev->mode_info;
395         int index;
396         u16 data_offset, size;
397         union umc_info *umc_info;
398         u8 frev, crev;
399         bool ecc_default_enabled = false;
400         u8 umc_config;
401         u32 umc_config1;
402
403         index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
404                         umc_info);
405
406         if (amdgpu_atom_parse_data_header(mode_info->atom_context,
407                                 index, &size, &frev, &crev, &data_offset)) {
408                 if (frev == 3) {
409                         umc_info = (union umc_info *)
410                                 (mode_info->atom_context->bios + data_offset);
411                         switch (crev) {
412                         case 1:
413                                 umc_config = le32_to_cpu(umc_info->v31.umc_config);
414                                 ecc_default_enabled =
415                                         (umc_config & UMC_CONFIG__DEFAULT_MEM_ECC_ENABLE) ? true : false;
416                                 break;
417                         case 2:
418                                 umc_config = le32_to_cpu(umc_info->v32.umc_config);
419                                 ecc_default_enabled =
420                                         (umc_config & UMC_CONFIG__DEFAULT_MEM_ECC_ENABLE) ? true : false;
421                                 break;
422                         case 3:
423                                 umc_config = le32_to_cpu(umc_info->v33.umc_config);
424                                 umc_config1 = le32_to_cpu(umc_info->v33.umc_config1);
425                                 ecc_default_enabled =
426                                         ((umc_config & UMC_CONFIG__DEFAULT_MEM_ECC_ENABLE) ||
427                                          (umc_config1 & UMC_CONFIG1__ENABLE_ECC_CAPABLE)) ? true : false;
428                                 break;
429                         default:
430                                 /* unsupported crev */
431                                 return false;
432                         }
433                 }
434         }
435
436         return ecc_default_enabled;
437 }
438
439 /*
440  * Helper function to query sram ecc capablity
441  *
442  * @adev: amdgpu_device pointer
443  *
444  * Return true if vbios supports sram ecc or false if not
445  */
446 bool amdgpu_atomfirmware_sram_ecc_supported(struct amdgpu_device *adev)
447 {
448         u32 fw_cap;
449
450         fw_cap = adev->mode_info.firmware_flags;
451
452         return (fw_cap & ATOM_FIRMWARE_CAP_SRAM_ECC) ? true : false;
453 }
454
455 /*
456  * Helper function to query dynamic boot config capability
457  *
458  * @adev: amdgpu_device pointer
459  *
460  * Return true if vbios supports dynamic boot config or false if not
461  */
462 bool amdgpu_atomfirmware_dynamic_boot_config_supported(struct amdgpu_device *adev)
463 {
464         u32 fw_cap;
465
466         fw_cap = adev->mode_info.firmware_flags;
467
468         return (fw_cap & ATOM_FIRMWARE_CAP_DYNAMIC_BOOT_CFG_ENABLE) ? true : false;
469 }
470
471 /*
472  * Helper function to query RAS EEPROM address
473  *
474  * @adev: amdgpu_device pointer
475  *
476  * Return true if vbios supports ras rom address reporting
477  */
478 bool amdgpu_atomfirmware_ras_rom_addr(struct amdgpu_device *adev, uint8_t* i2c_address)
479 {
480         struct amdgpu_mode_info *mode_info = &adev->mode_info;
481         int index;
482         u16 data_offset, size;
483         union firmware_info *firmware_info;
484         u8 frev, crev;
485
486         if (i2c_address == NULL)
487                 return false;
488
489         *i2c_address = 0;
490
491         index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
492                         firmwareinfo);
493
494         if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context,
495                                 index, &size, &frev, &crev, &data_offset)) {
496                 /* support firmware_info 3.4 + */
497                 if ((frev == 3 && crev >=4) || (frev > 3)) {
498                         firmware_info = (union firmware_info *)
499                                 (mode_info->atom_context->bios + data_offset);
500                         *i2c_address = firmware_info->v34.ras_rom_i2c_slave_addr;
501                 }
502         }
503
504         if (*i2c_address != 0)
505                 return true;
506
507         return false;
508 }
509
510
511 union smu_info {
512         struct atom_smu_info_v3_1 v31;
513 };
514
515 int amdgpu_atomfirmware_get_clock_info(struct amdgpu_device *adev)
516 {
517         struct amdgpu_mode_info *mode_info = &adev->mode_info;
518         struct amdgpu_pll *spll = &adev->clock.spll;
519         struct amdgpu_pll *mpll = &adev->clock.mpll;
520         uint8_t frev, crev;
521         uint16_t data_offset;
522         int ret = -EINVAL, index;
523
524         index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
525                                             firmwareinfo);
526         if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
527                                    &frev, &crev, &data_offset)) {
528                 union firmware_info *firmware_info =
529                         (union firmware_info *)(mode_info->atom_context->bios +
530                                                 data_offset);
531
532                 adev->clock.default_sclk =
533                         le32_to_cpu(firmware_info->v31.bootup_sclk_in10khz);
534                 adev->clock.default_mclk =
535                         le32_to_cpu(firmware_info->v31.bootup_mclk_in10khz);
536
537                 adev->pm.current_sclk = adev->clock.default_sclk;
538                 adev->pm.current_mclk = adev->clock.default_mclk;
539
540                 ret = 0;
541         }
542
543         index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
544                                             smu_info);
545         if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
546                                    &frev, &crev, &data_offset)) {
547                 union smu_info *smu_info =
548                         (union smu_info *)(mode_info->atom_context->bios +
549                                            data_offset);
550
551                 /* system clock */
552                 spll->reference_freq = le32_to_cpu(smu_info->v31.core_refclk_10khz);
553
554                 spll->reference_div = 0;
555                 spll->min_post_div = 1;
556                 spll->max_post_div = 1;
557                 spll->min_ref_div = 2;
558                 spll->max_ref_div = 0xff;
559                 spll->min_feedback_div = 4;
560                 spll->max_feedback_div = 0xff;
561                 spll->best_vco = 0;
562
563                 ret = 0;
564         }
565
566         index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
567                                             umc_info);
568         if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
569                                    &frev, &crev, &data_offset)) {
570                 union umc_info *umc_info =
571                         (union umc_info *)(mode_info->atom_context->bios +
572                                            data_offset);
573
574                 /* memory clock */
575                 mpll->reference_freq = le32_to_cpu(umc_info->v31.mem_refclk_10khz);
576
577                 mpll->reference_div = 0;
578                 mpll->min_post_div = 1;
579                 mpll->max_post_div = 1;
580                 mpll->min_ref_div = 2;
581                 mpll->max_ref_div = 0xff;
582                 mpll->min_feedback_div = 4;
583                 mpll->max_feedback_div = 0xff;
584                 mpll->best_vco = 0;
585
586                 ret = 0;
587         }
588
589         /* if asic is Navi+, the rlc reference clock is used for system clock
590          * from vbios gfx_info table */
591         if (adev->asic_type >= CHIP_NAVI10) {
592                 index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
593                                                    gfx_info);
594                 if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
595                                           &frev, &crev, &data_offset)) {
596                         struct atom_gfx_info_v2_2 *gfx_info = (struct atom_gfx_info_v2_2*)
597                                 (mode_info->atom_context->bios + data_offset);
598                         if ((frev == 2) && (crev >= 2))
599                                 spll->reference_freq = le32_to_cpu(gfx_info->rlc_gpu_timer_refclk);
600                         ret = 0;
601                 }
602         }
603
604         return ret;
605 }
606
607 union gfx_info {
608         struct atom_gfx_info_v2_4 v24;
609         struct atom_gfx_info_v2_7 v27;
610 };
611
612 int amdgpu_atomfirmware_get_gfx_info(struct amdgpu_device *adev)
613 {
614         struct amdgpu_mode_info *mode_info = &adev->mode_info;
615         int index;
616         uint8_t frev, crev;
617         uint16_t data_offset;
618
619         index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
620                                             gfx_info);
621         if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
622                                    &frev, &crev, &data_offset)) {
623                 union gfx_info *gfx_info = (union gfx_info *)
624                         (mode_info->atom_context->bios + data_offset);
625                 switch (crev) {
626                 case 4:
627                         adev->gfx.config.max_shader_engines = gfx_info->v24.max_shader_engines;
628                         adev->gfx.config.max_cu_per_sh = gfx_info->v24.max_cu_per_sh;
629                         adev->gfx.config.max_sh_per_se = gfx_info->v24.max_sh_per_se;
630                         adev->gfx.config.max_backends_per_se = gfx_info->v24.max_backends_per_se;
631                         adev->gfx.config.max_texture_channel_caches = gfx_info->v24.max_texture_channel_caches;
632                         adev->gfx.config.max_gprs = le16_to_cpu(gfx_info->v24.gc_num_gprs);
633                         adev->gfx.config.max_gs_threads = gfx_info->v24.gc_num_max_gs_thds;
634                         adev->gfx.config.gs_vgt_table_depth = gfx_info->v24.gc_gs_table_depth;
635                         adev->gfx.config.gs_prim_buffer_depth =
636                                 le16_to_cpu(gfx_info->v24.gc_gsprim_buff_depth);
637                         adev->gfx.config.double_offchip_lds_buf =
638                                 gfx_info->v24.gc_double_offchip_lds_buffer;
639                         adev->gfx.cu_info.wave_front_size = le16_to_cpu(gfx_info->v24.gc_wave_size);
640                         adev->gfx.cu_info.max_waves_per_simd = le16_to_cpu(gfx_info->v24.gc_max_waves_per_simd);
641                         adev->gfx.cu_info.max_scratch_slots_per_cu = gfx_info->v24.gc_max_scratch_slots_per_cu;
642                         adev->gfx.cu_info.lds_size = le16_to_cpu(gfx_info->v24.gc_lds_size);
643                         return 0;
644                 case 7:
645                         adev->gfx.config.max_shader_engines = gfx_info->v27.max_shader_engines;
646                         adev->gfx.config.max_cu_per_sh = gfx_info->v27.max_cu_per_sh;
647                         adev->gfx.config.max_sh_per_se = gfx_info->v27.max_sh_per_se;
648                         adev->gfx.config.max_backends_per_se = gfx_info->v27.max_backends_per_se;
649                         adev->gfx.config.max_texture_channel_caches = gfx_info->v27.max_texture_channel_caches;
650                         adev->gfx.config.max_gprs = le16_to_cpu(gfx_info->v27.gc_num_gprs);
651                         adev->gfx.config.max_gs_threads = gfx_info->v27.gc_num_max_gs_thds;
652                         adev->gfx.config.gs_vgt_table_depth = gfx_info->v27.gc_gs_table_depth;
653                         adev->gfx.config.gs_prim_buffer_depth = le16_to_cpu(gfx_info->v27.gc_gsprim_buff_depth);
654                         adev->gfx.config.double_offchip_lds_buf = gfx_info->v27.gc_double_offchip_lds_buffer;
655                         adev->gfx.cu_info.wave_front_size = le16_to_cpu(gfx_info->v27.gc_wave_size);
656                         adev->gfx.cu_info.max_waves_per_simd = le16_to_cpu(gfx_info->v27.gc_max_waves_per_simd);
657                         adev->gfx.cu_info.max_scratch_slots_per_cu = gfx_info->v27.gc_max_scratch_slots_per_cu;
658                         adev->gfx.cu_info.lds_size = le16_to_cpu(gfx_info->v27.gc_lds_size);
659                         return 0;
660                 default:
661                         return -EINVAL;
662                 }
663
664         }
665         return -EINVAL;
666 }
667
668 /*
669  * Helper function to query two stage mem training capability
670  *
671  * @adev: amdgpu_device pointer
672  *
673  * Return true if two stage mem training is supported or false if not
674  */
675 bool amdgpu_atomfirmware_mem_training_supported(struct amdgpu_device *adev)
676 {
677         u32 fw_cap;
678
679         fw_cap = adev->mode_info.firmware_flags;
680
681         return (fw_cap & ATOM_FIRMWARE_CAP_ENABLE_2STAGE_BIST_TRAINING) ? true : false;
682 }
683
684 int amdgpu_atomfirmware_get_fw_reserved_fb_size(struct amdgpu_device *adev)
685 {
686         struct atom_context *ctx = adev->mode_info.atom_context;
687         union firmware_info *firmware_info;
688         int index;
689         u16 data_offset, size;
690         u8 frev, crev;
691         int fw_reserved_fb_size;
692
693         index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
694                         firmwareinfo);
695
696         if (!amdgpu_atom_parse_data_header(ctx, index, &size,
697                                 &frev, &crev, &data_offset))
698                 /* fail to parse data_header */
699                 return 0;
700
701         firmware_info = (union firmware_info *)(ctx->bios + data_offset);
702
703         if (frev !=3)
704                 return -EINVAL;
705
706         switch (crev) {
707         case 4:
708                 fw_reserved_fb_size =
709                         (firmware_info->v34.fw_reserved_size_in_kb << 10);
710                 break;
711         default:
712                 fw_reserved_fb_size = 0;
713                 break;
714         }
715
716         return fw_reserved_fb_size;
717 }