amdgpu_atombios.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/amdgpu_drm.h>
  28. #include "amdgpu.h"
  29. #include "amdgpu_atombios.h"
  30. #include "amdgpu_i2c.h"
  31. #include "atom.h"
  32. #include "atom-bits.h"
  33. #include "atombios_encoders.h"
  34. #include "bif/bif_4_1_d.h"
  35. static void amdgpu_atombios_lookup_i2c_gpio_quirks(struct amdgpu_device *adev,
  36. ATOM_GPIO_I2C_ASSIGMENT *gpio,
  37. u8 index)
  38. {
  39. }
  40. static struct amdgpu_i2c_bus_rec amdgpu_atombios_get_bus_rec_for_i2c_gpio(ATOM_GPIO_I2C_ASSIGMENT *gpio)
  41. {
  42. struct amdgpu_i2c_bus_rec i2c;
  43. memset(&i2c, 0, sizeof(struct amdgpu_i2c_bus_rec));
  44. i2c.mask_clk_reg = le16_to_cpu(gpio->usClkMaskRegisterIndex);
  45. i2c.mask_data_reg = le16_to_cpu(gpio->usDataMaskRegisterIndex);
  46. i2c.en_clk_reg = le16_to_cpu(gpio->usClkEnRegisterIndex);
  47. i2c.en_data_reg = le16_to_cpu(gpio->usDataEnRegisterIndex);
  48. i2c.y_clk_reg = le16_to_cpu(gpio->usClkY_RegisterIndex);
  49. i2c.y_data_reg = le16_to_cpu(gpio->usDataY_RegisterIndex);
  50. i2c.a_clk_reg = le16_to_cpu(gpio->usClkA_RegisterIndex);
  51. i2c.a_data_reg = le16_to_cpu(gpio->usDataA_RegisterIndex);
  52. i2c.mask_clk_mask = (1 << gpio->ucClkMaskShift);
  53. i2c.mask_data_mask = (1 << gpio->ucDataMaskShift);
  54. i2c.en_clk_mask = (1 << gpio->ucClkEnShift);
  55. i2c.en_data_mask = (1 << gpio->ucDataEnShift);
  56. i2c.y_clk_mask = (1 << gpio->ucClkY_Shift);
  57. i2c.y_data_mask = (1 << gpio->ucDataY_Shift);
  58. i2c.a_clk_mask = (1 << gpio->ucClkA_Shift);
  59. i2c.a_data_mask = (1 << gpio->ucDataA_Shift);
  60. if (gpio->sucI2cId.sbfAccess.bfHW_Capable)
  61. i2c.hw_capable = true;
  62. else
  63. i2c.hw_capable = false;
  64. if (gpio->sucI2cId.ucAccess == 0xa0)
  65. i2c.mm_i2c = true;
  66. else
  67. i2c.mm_i2c = false;
  68. i2c.i2c_id = gpio->sucI2cId.ucAccess;
  69. if (i2c.mask_clk_reg)
  70. i2c.valid = true;
  71. else
  72. i2c.valid = false;
  73. return i2c;
  74. }
  75. struct amdgpu_i2c_bus_rec amdgpu_atombios_lookup_i2c_gpio(struct amdgpu_device *adev,
  76. uint8_t id)
  77. {
  78. struct atom_context *ctx = adev->mode_info.atom_context;
  79. ATOM_GPIO_I2C_ASSIGMENT *gpio;
  80. struct amdgpu_i2c_bus_rec i2c;
  81. int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
  82. struct _ATOM_GPIO_I2C_INFO *i2c_info;
  83. uint16_t data_offset, size;
  84. int i, num_indices;
  85. memset(&i2c, 0, sizeof(struct amdgpu_i2c_bus_rec));
  86. i2c.valid = false;
  87. if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
  88. i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
  89. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  90. sizeof(ATOM_GPIO_I2C_ASSIGMENT);
  91. gpio = &i2c_info->asGPIO_Info[0];
  92. for (i = 0; i < num_indices; i++) {
  93. amdgpu_atombios_lookup_i2c_gpio_quirks(adev, gpio, i);
  94. if (gpio->sucI2cId.ucAccess == id) {
  95. i2c = amdgpu_atombios_get_bus_rec_for_i2c_gpio(gpio);
  96. break;
  97. }
  98. gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
  99. ((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
  100. }
  101. }
  102. return i2c;
  103. }
  104. void amdgpu_atombios_i2c_init(struct amdgpu_device *adev)
  105. {
  106. struct atom_context *ctx = adev->mode_info.atom_context;
  107. ATOM_GPIO_I2C_ASSIGMENT *gpio;
  108. struct amdgpu_i2c_bus_rec i2c;
  109. int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
  110. struct _ATOM_GPIO_I2C_INFO *i2c_info;
  111. uint16_t data_offset, size;
  112. int i, num_indices;
  113. char stmp[32];
  114. if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
  115. i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
  116. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  117. sizeof(ATOM_GPIO_I2C_ASSIGMENT);
  118. gpio = &i2c_info->asGPIO_Info[0];
  119. for (i = 0; i < num_indices; i++) {
  120. amdgpu_atombios_lookup_i2c_gpio_quirks(adev, gpio, i);
  121. i2c = amdgpu_atombios_get_bus_rec_for_i2c_gpio(gpio);
  122. if (i2c.valid) {
  123. sprintf(stmp, "0x%x", i2c.i2c_id);
  124. adev->i2c_bus[i] = amdgpu_i2c_create(adev->ddev, &i2c, stmp);
  125. }
  126. gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
  127. ((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
  128. }
  129. }
  130. }
  131. struct amdgpu_gpio_rec
  132. amdgpu_atombios_lookup_gpio(struct amdgpu_device *adev,
  133. u8 id)
  134. {
  135. struct atom_context *ctx = adev->mode_info.atom_context;
  136. struct amdgpu_gpio_rec gpio;
  137. int index = GetIndexIntoMasterTable(DATA, GPIO_Pin_LUT);
  138. struct _ATOM_GPIO_PIN_LUT *gpio_info;
  139. ATOM_GPIO_PIN_ASSIGNMENT *pin;
  140. u16 data_offset, size;
  141. int i, num_indices;
  142. memset(&gpio, 0, sizeof(struct amdgpu_gpio_rec));
  143. gpio.valid = false;
  144. if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
  145. gpio_info = (struct _ATOM_GPIO_PIN_LUT *)(ctx->bios + data_offset);
  146. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  147. sizeof(ATOM_GPIO_PIN_ASSIGNMENT);
  148. pin = gpio_info->asGPIO_Pin;
  149. for (i = 0; i < num_indices; i++) {
  150. if (id == pin->ucGPIO_ID) {
  151. gpio.id = pin->ucGPIO_ID;
  152. gpio.reg = le16_to_cpu(pin->usGpioPin_AIndex);
  153. gpio.shift = pin->ucGpioPinBitShift;
  154. gpio.mask = (1 << pin->ucGpioPinBitShift);
  155. gpio.valid = true;
  156. break;
  157. }
  158. pin = (ATOM_GPIO_PIN_ASSIGNMENT *)
  159. ((u8 *)pin + sizeof(ATOM_GPIO_PIN_ASSIGNMENT));
  160. }
  161. }
  162. return gpio;
  163. }
  164. static struct amdgpu_hpd
  165. amdgpu_atombios_get_hpd_info_from_gpio(struct amdgpu_device *adev,
  166. struct amdgpu_gpio_rec *gpio)
  167. {
  168. struct amdgpu_hpd hpd;
  169. u32 reg;
  170. memset(&hpd, 0, sizeof(struct amdgpu_hpd));
  171. reg = amdgpu_display_hpd_get_gpio_reg(adev);
  172. hpd.gpio = *gpio;
  173. if (gpio->reg == reg) {
  174. switch(gpio->mask) {
  175. case (1 << 0):
  176. hpd.hpd = AMDGPU_HPD_1;
  177. break;
  178. case (1 << 8):
  179. hpd.hpd = AMDGPU_HPD_2;
  180. break;
  181. case (1 << 16):
  182. hpd.hpd = AMDGPU_HPD_3;
  183. break;
  184. case (1 << 24):
  185. hpd.hpd = AMDGPU_HPD_4;
  186. break;
  187. case (1 << 26):
  188. hpd.hpd = AMDGPU_HPD_5;
  189. break;
  190. case (1 << 28):
  191. hpd.hpd = AMDGPU_HPD_6;
  192. break;
  193. default:
  194. hpd.hpd = AMDGPU_HPD_NONE;
  195. break;
  196. }
  197. } else
  198. hpd.hpd = AMDGPU_HPD_NONE;
  199. return hpd;
  200. }
  201. static bool amdgpu_atombios_apply_quirks(struct amdgpu_device *adev,
  202. uint32_t supported_device,
  203. int *connector_type,
  204. struct amdgpu_i2c_bus_rec *i2c_bus,
  205. uint16_t *line_mux,
  206. struct amdgpu_hpd *hpd)
  207. {
  208. return true;
  209. }
  210. static const int object_connector_convert[] = {
  211. DRM_MODE_CONNECTOR_Unknown,
  212. DRM_MODE_CONNECTOR_DVII,
  213. DRM_MODE_CONNECTOR_DVII,
  214. DRM_MODE_CONNECTOR_DVID,
  215. DRM_MODE_CONNECTOR_DVID,
  216. DRM_MODE_CONNECTOR_VGA,
  217. DRM_MODE_CONNECTOR_Composite,
  218. DRM_MODE_CONNECTOR_SVIDEO,
  219. DRM_MODE_CONNECTOR_Unknown,
  220. DRM_MODE_CONNECTOR_Unknown,
  221. DRM_MODE_CONNECTOR_9PinDIN,
  222. DRM_MODE_CONNECTOR_Unknown,
  223. DRM_MODE_CONNECTOR_HDMIA,
  224. DRM_MODE_CONNECTOR_HDMIB,
  225. DRM_MODE_CONNECTOR_LVDS,
  226. DRM_MODE_CONNECTOR_9PinDIN,
  227. DRM_MODE_CONNECTOR_Unknown,
  228. DRM_MODE_CONNECTOR_Unknown,
  229. DRM_MODE_CONNECTOR_Unknown,
  230. DRM_MODE_CONNECTOR_DisplayPort,
  231. DRM_MODE_CONNECTOR_eDP,
  232. DRM_MODE_CONNECTOR_Unknown
  233. };
  234. bool amdgpu_atombios_get_connector_info_from_object_table(struct amdgpu_device *adev)
  235. {
  236. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  237. struct atom_context *ctx = mode_info->atom_context;
  238. int index = GetIndexIntoMasterTable(DATA, Object_Header);
  239. u16 size, data_offset;
  240. u8 frev, crev;
  241. ATOM_CONNECTOR_OBJECT_TABLE *con_obj;
  242. ATOM_ENCODER_OBJECT_TABLE *enc_obj;
  243. ATOM_OBJECT_TABLE *router_obj;
  244. ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
  245. ATOM_OBJECT_HEADER *obj_header;
  246. int i, j, k, path_size, device_support;
  247. int connector_type;
  248. u16 conn_id, connector_object_id;
  249. struct amdgpu_i2c_bus_rec ddc_bus;
  250. struct amdgpu_router router;
  251. struct amdgpu_gpio_rec gpio;
  252. struct amdgpu_hpd hpd;
  253. if (!amdgpu_atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
  254. return false;
  255. if (crev < 2)
  256. return false;
  257. obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
  258. path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
  259. (ctx->bios + data_offset +
  260. le16_to_cpu(obj_header->usDisplayPathTableOffset));
  261. con_obj = (ATOM_CONNECTOR_OBJECT_TABLE *)
  262. (ctx->bios + data_offset +
  263. le16_to_cpu(obj_header->usConnectorObjectTableOffset));
  264. enc_obj = (ATOM_ENCODER_OBJECT_TABLE *)
  265. (ctx->bios + data_offset +
  266. le16_to_cpu(obj_header->usEncoderObjectTableOffset));
  267. router_obj = (ATOM_OBJECT_TABLE *)
  268. (ctx->bios + data_offset +
  269. le16_to_cpu(obj_header->usRouterObjectTableOffset));
  270. device_support = le16_to_cpu(obj_header->usDeviceSupport);
  271. path_size = 0;
  272. for (i = 0; i < path_obj->ucNumOfDispPath; i++) {
  273. uint8_t *addr = (uint8_t *) path_obj->asDispPath;
  274. ATOM_DISPLAY_OBJECT_PATH *path;
  275. addr += path_size;
  276. path = (ATOM_DISPLAY_OBJECT_PATH *) addr;
  277. path_size += le16_to_cpu(path->usSize);
  278. if (device_support & le16_to_cpu(path->usDeviceTag)) {
  279. uint8_t con_obj_id, con_obj_num, con_obj_type;
  280. con_obj_id =
  281. (le16_to_cpu(path->usConnObjectId) & OBJECT_ID_MASK)
  282. >> OBJECT_ID_SHIFT;
  283. con_obj_num =
  284. (le16_to_cpu(path->usConnObjectId) & ENUM_ID_MASK)
  285. >> ENUM_ID_SHIFT;
  286. con_obj_type =
  287. (le16_to_cpu(path->usConnObjectId) &
  288. OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
  289. /* Skip TV/CV support */
  290. if ((le16_to_cpu(path->usDeviceTag) ==
  291. ATOM_DEVICE_TV1_SUPPORT) ||
  292. (le16_to_cpu(path->usDeviceTag) ==
  293. ATOM_DEVICE_CV_SUPPORT))
  294. continue;
  295. if (con_obj_id >= ARRAY_SIZE(object_connector_convert)) {
  296. DRM_ERROR("invalid con_obj_id %d for device tag 0x%04x\n",
  297. con_obj_id, le16_to_cpu(path->usDeviceTag));
  298. continue;
  299. }
  300. connector_type =
  301. object_connector_convert[con_obj_id];
  302. connector_object_id = con_obj_id;
  303. if (connector_type == DRM_MODE_CONNECTOR_Unknown)
  304. continue;
  305. router.ddc_valid = false;
  306. router.cd_valid = false;
  307. for (j = 0; j < ((le16_to_cpu(path->usSize) - 8) / 2); j++) {
  308. uint8_t grph_obj_id, grph_obj_num, grph_obj_type;
  309. grph_obj_id =
  310. (le16_to_cpu(path->usGraphicObjIds[j]) &
  311. OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
  312. grph_obj_num =
  313. (le16_to_cpu(path->usGraphicObjIds[j]) &
  314. ENUM_ID_MASK) >> ENUM_ID_SHIFT;
  315. grph_obj_type =
  316. (le16_to_cpu(path->usGraphicObjIds[j]) &
  317. OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
  318. if (grph_obj_type == GRAPH_OBJECT_TYPE_ENCODER) {
  319. for (k = 0; k < enc_obj->ucNumberOfObjects; k++) {
  320. u16 encoder_obj = le16_to_cpu(enc_obj->asObjects[k].usObjectID);
  321. if (le16_to_cpu(path->usGraphicObjIds[j]) == encoder_obj) {
  322. ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
  323. (ctx->bios + data_offset +
  324. le16_to_cpu(enc_obj->asObjects[k].usRecordOffset));
  325. ATOM_ENCODER_CAP_RECORD *cap_record;
  326. u16 caps = 0;
  327. while (record->ucRecordSize > 0 &&
  328. record->ucRecordType > 0 &&
  329. record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
  330. switch (record->ucRecordType) {
  331. case ATOM_ENCODER_CAP_RECORD_TYPE:
  332. cap_record =(ATOM_ENCODER_CAP_RECORD *)
  333. record;
  334. caps = le16_to_cpu(cap_record->usEncoderCap);
  335. break;
  336. }
  337. record = (ATOM_COMMON_RECORD_HEADER *)
  338. ((char *)record + record->ucRecordSize);
  339. }
  340. amdgpu_display_add_encoder(adev, encoder_obj,
  341. le16_to_cpu(path->usDeviceTag),
  342. caps);
  343. }
  344. }
  345. } else if (grph_obj_type == GRAPH_OBJECT_TYPE_ROUTER) {
  346. for (k = 0; k < router_obj->ucNumberOfObjects; k++) {
  347. u16 router_obj_id = le16_to_cpu(router_obj->asObjects[k].usObjectID);
  348. if (le16_to_cpu(path->usGraphicObjIds[j]) == router_obj_id) {
  349. ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
  350. (ctx->bios + data_offset +
  351. le16_to_cpu(router_obj->asObjects[k].usRecordOffset));
  352. ATOM_I2C_RECORD *i2c_record;
  353. ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
  354. ATOM_ROUTER_DDC_PATH_SELECT_RECORD *ddc_path;
  355. ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *cd_path;
  356. ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *router_src_dst_table =
  357. (ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *)
  358. (ctx->bios + data_offset +
  359. le16_to_cpu(router_obj->asObjects[k].usSrcDstTableOffset));
  360. u8 *num_dst_objs = (u8 *)
  361. ((u8 *)router_src_dst_table + 1 +
  362. (router_src_dst_table->ucNumberOfSrc * 2));
  363. u16 *dst_objs = (u16 *)(num_dst_objs + 1);
  364. int enum_id;
  365. router.router_id = router_obj_id;
  366. for (enum_id = 0; enum_id < (*num_dst_objs); enum_id++) {
  367. if (le16_to_cpu(path->usConnObjectId) ==
  368. le16_to_cpu(dst_objs[enum_id]))
  369. break;
  370. }
  371. while (record->ucRecordSize > 0 &&
  372. record->ucRecordType > 0 &&
  373. record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
  374. switch (record->ucRecordType) {
  375. case ATOM_I2C_RECORD_TYPE:
  376. i2c_record =
  377. (ATOM_I2C_RECORD *)
  378. record;
  379. i2c_config =
  380. (ATOM_I2C_ID_CONFIG_ACCESS *)
  381. &i2c_record->sucI2cId;
  382. router.i2c_info =
  383. amdgpu_atombios_lookup_i2c_gpio(adev,
  384. i2c_config->
  385. ucAccess);
  386. router.i2c_addr = i2c_record->ucI2CAddr >> 1;
  387. break;
  388. case ATOM_ROUTER_DDC_PATH_SELECT_RECORD_TYPE:
  389. ddc_path = (ATOM_ROUTER_DDC_PATH_SELECT_RECORD *)
  390. record;
  391. router.ddc_valid = true;
  392. router.ddc_mux_type = ddc_path->ucMuxType;
  393. router.ddc_mux_control_pin = ddc_path->ucMuxControlPin;
  394. router.ddc_mux_state = ddc_path->ucMuxState[enum_id];
  395. break;
  396. case ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD_TYPE:
  397. cd_path = (ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *)
  398. record;
  399. router.cd_valid = true;
  400. router.cd_mux_type = cd_path->ucMuxType;
  401. router.cd_mux_control_pin = cd_path->ucMuxControlPin;
  402. router.cd_mux_state = cd_path->ucMuxState[enum_id];
  403. break;
  404. }
  405. record = (ATOM_COMMON_RECORD_HEADER *)
  406. ((char *)record + record->ucRecordSize);
  407. }
  408. }
  409. }
  410. }
  411. }
  412. /* look up gpio for ddc, hpd */
  413. ddc_bus.valid = false;
  414. hpd.hpd = AMDGPU_HPD_NONE;
  415. if ((le16_to_cpu(path->usDeviceTag) &
  416. (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT)) == 0) {
  417. for (j = 0; j < con_obj->ucNumberOfObjects; j++) {
  418. if (le16_to_cpu(path->usConnObjectId) ==
  419. le16_to_cpu(con_obj->asObjects[j].
  420. usObjectID)) {
  421. ATOM_COMMON_RECORD_HEADER
  422. *record =
  423. (ATOM_COMMON_RECORD_HEADER
  424. *)
  425. (ctx->bios + data_offset +
  426. le16_to_cpu(con_obj->
  427. asObjects[j].
  428. usRecordOffset));
  429. ATOM_I2C_RECORD *i2c_record;
  430. ATOM_HPD_INT_RECORD *hpd_record;
  431. ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
  432. while (record->ucRecordSize > 0 &&
  433. record->ucRecordType > 0 &&
  434. record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
  435. switch (record->ucRecordType) {
  436. case ATOM_I2C_RECORD_TYPE:
  437. i2c_record =
  438. (ATOM_I2C_RECORD *)
  439. record;
  440. i2c_config =
  441. (ATOM_I2C_ID_CONFIG_ACCESS *)
  442. &i2c_record->sucI2cId;
  443. ddc_bus = amdgpu_atombios_lookup_i2c_gpio(adev,
  444. i2c_config->
  445. ucAccess);
  446. break;
  447. case ATOM_HPD_INT_RECORD_TYPE:
  448. hpd_record =
  449. (ATOM_HPD_INT_RECORD *)
  450. record;
  451. gpio = amdgpu_atombios_lookup_gpio(adev,
  452. hpd_record->ucHPDIntGPIOID);
  453. hpd = amdgpu_atombios_get_hpd_info_from_gpio(adev, &gpio);
  454. hpd.plugged_state = hpd_record->ucPlugged_PinState;
  455. break;
  456. }
  457. record =
  458. (ATOM_COMMON_RECORD_HEADER
  459. *) ((char *)record
  460. +
  461. record->
  462. ucRecordSize);
  463. }
  464. break;
  465. }
  466. }
  467. }
  468. /* needed for aux chan transactions */
  469. ddc_bus.hpd = hpd.hpd;
  470. conn_id = le16_to_cpu(path->usConnObjectId);
  471. if (!amdgpu_atombios_apply_quirks
  472. (adev, le16_to_cpu(path->usDeviceTag), &connector_type,
  473. &ddc_bus, &conn_id, &hpd))
  474. continue;
  475. amdgpu_display_add_connector(adev,
  476. conn_id,
  477. le16_to_cpu(path->usDeviceTag),
  478. connector_type, &ddc_bus,
  479. connector_object_id,
  480. &hpd,
  481. &router);
  482. }
  483. }
  484. amdgpu_link_encoder_connector(adev->ddev);
  485. return true;
  486. }
  487. union firmware_info {
  488. ATOM_FIRMWARE_INFO info;
  489. ATOM_FIRMWARE_INFO_V1_2 info_12;
  490. ATOM_FIRMWARE_INFO_V1_3 info_13;
  491. ATOM_FIRMWARE_INFO_V1_4 info_14;
  492. ATOM_FIRMWARE_INFO_V2_1 info_21;
  493. ATOM_FIRMWARE_INFO_V2_2 info_22;
  494. };
  495. int amdgpu_atombios_get_clock_info(struct amdgpu_device *adev)
  496. {
  497. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  498. int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
  499. uint8_t frev, crev;
  500. uint16_t data_offset;
  501. int ret = -EINVAL;
  502. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  503. &frev, &crev, &data_offset)) {
  504. int i;
  505. struct amdgpu_pll *ppll = &adev->clock.ppll[0];
  506. struct amdgpu_pll *spll = &adev->clock.spll;
  507. struct amdgpu_pll *mpll = &adev->clock.mpll;
  508. union firmware_info *firmware_info =
  509. (union firmware_info *)(mode_info->atom_context->bios +
  510. data_offset);
  511. /* pixel clocks */
  512. ppll->reference_freq =
  513. le16_to_cpu(firmware_info->info.usReferenceClock);
  514. ppll->reference_div = 0;
  515. ppll->pll_out_min =
  516. le32_to_cpu(firmware_info->info_12.ulMinPixelClockPLL_Output);
  517. ppll->pll_out_max =
  518. le32_to_cpu(firmware_info->info.ulMaxPixelClockPLL_Output);
  519. ppll->lcd_pll_out_min =
  520. le16_to_cpu(firmware_info->info_14.usLcdMinPixelClockPLL_Output) * 100;
  521. if (ppll->lcd_pll_out_min == 0)
  522. ppll->lcd_pll_out_min = ppll->pll_out_min;
  523. ppll->lcd_pll_out_max =
  524. le16_to_cpu(firmware_info->info_14.usLcdMaxPixelClockPLL_Output) * 100;
  525. if (ppll->lcd_pll_out_max == 0)
  526. ppll->lcd_pll_out_max = ppll->pll_out_max;
  527. if (ppll->pll_out_min == 0)
  528. ppll->pll_out_min = 64800;
  529. ppll->pll_in_min =
  530. le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Input);
  531. ppll->pll_in_max =
  532. le16_to_cpu(firmware_info->info.usMaxPixelClockPLL_Input);
  533. ppll->min_post_div = 2;
  534. ppll->max_post_div = 0x7f;
  535. ppll->min_frac_feedback_div = 0;
  536. ppll->max_frac_feedback_div = 9;
  537. ppll->min_ref_div = 2;
  538. ppll->max_ref_div = 0x3ff;
  539. ppll->min_feedback_div = 4;
  540. ppll->max_feedback_div = 0xfff;
  541. ppll->best_vco = 0;
  542. for (i = 1; i < AMDGPU_MAX_PPLL; i++)
  543. adev->clock.ppll[i] = *ppll;
  544. /* system clock */
  545. spll->reference_freq =
  546. le16_to_cpu(firmware_info->info_21.usCoreReferenceClock);
  547. spll->reference_div = 0;
  548. spll->pll_out_min =
  549. le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Output);
  550. spll->pll_out_max =
  551. le32_to_cpu(firmware_info->info.ulMaxEngineClockPLL_Output);
  552. /* ??? */
  553. if (spll->pll_out_min == 0)
  554. spll->pll_out_min = 64800;
  555. spll->pll_in_min =
  556. le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Input);
  557. spll->pll_in_max =
  558. le16_to_cpu(firmware_info->info.usMaxEngineClockPLL_Input);
  559. spll->min_post_div = 1;
  560. spll->max_post_div = 1;
  561. spll->min_ref_div = 2;
  562. spll->max_ref_div = 0xff;
  563. spll->min_feedback_div = 4;
  564. spll->max_feedback_div = 0xff;
  565. spll->best_vco = 0;
  566. /* memory clock */
  567. mpll->reference_freq =
  568. le16_to_cpu(firmware_info->info_21.usMemoryReferenceClock);
  569. mpll->reference_div = 0;
  570. mpll->pll_out_min =
  571. le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Output);
  572. mpll->pll_out_max =
  573. le32_to_cpu(firmware_info->info.ulMaxMemoryClockPLL_Output);
  574. /* ??? */
  575. if (mpll->pll_out_min == 0)
  576. mpll->pll_out_min = 64800;
  577. mpll->pll_in_min =
  578. le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Input);
  579. mpll->pll_in_max =
  580. le16_to_cpu(firmware_info->info.usMaxMemoryClockPLL_Input);
  581. adev->clock.default_sclk =
  582. le32_to_cpu(firmware_info->info.ulDefaultEngineClock);
  583. adev->clock.default_mclk =
  584. le32_to_cpu(firmware_info->info.ulDefaultMemoryClock);
  585. mpll->min_post_div = 1;
  586. mpll->max_post_div = 1;
  587. mpll->min_ref_div = 2;
  588. mpll->max_ref_div = 0xff;
  589. mpll->min_feedback_div = 4;
  590. mpll->max_feedback_div = 0xff;
  591. mpll->best_vco = 0;
  592. /* disp clock */
  593. adev->clock.default_dispclk =
  594. le32_to_cpu(firmware_info->info_21.ulDefaultDispEngineClkFreq);
  595. /* set a reasonable default for DP */
  596. if (adev->clock.default_dispclk < 53900) {
  597. DRM_INFO("Changing default dispclk from %dMhz to 600Mhz\n",
  598. adev->clock.default_dispclk / 100);
  599. adev->clock.default_dispclk = 60000;
  600. } else if (adev->clock.default_dispclk <= 60000) {
  601. DRM_INFO("Changing default dispclk from %dMhz to 625Mhz\n",
  602. adev->clock.default_dispclk / 100);
  603. adev->clock.default_dispclk = 62500;
  604. }
  605. adev->clock.dp_extclk =
  606. le16_to_cpu(firmware_info->info_21.usUniphyDPModeExtClkFreq);
  607. adev->clock.current_dispclk = adev->clock.default_dispclk;
  608. adev->clock.max_pixel_clock = le16_to_cpu(firmware_info->info.usMaxPixelClock);
  609. if (adev->clock.max_pixel_clock == 0)
  610. adev->clock.max_pixel_clock = 40000;
  611. /* not technically a clock, but... */
  612. adev->mode_info.firmware_flags =
  613. le16_to_cpu(firmware_info->info.usFirmwareCapability.susAccess);
  614. ret = 0;
  615. }
  616. adev->pm.current_sclk = adev->clock.default_sclk;
  617. adev->pm.current_mclk = adev->clock.default_mclk;
  618. return ret;
  619. }
  620. union igp_info {
  621. struct _ATOM_INTEGRATED_SYSTEM_INFO info;
  622. struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
  623. struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
  624. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
  625. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
  626. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_9 info_9;
  627. };
  628. static void amdgpu_atombios_get_igp_ss_overrides(struct amdgpu_device *adev,
  629. struct amdgpu_atom_ss *ss,
  630. int id)
  631. {
  632. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  633. int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
  634. u16 data_offset, size;
  635. union igp_info *igp_info;
  636. u8 frev, crev;
  637. u16 percentage = 0, rate = 0;
  638. /* get any igp specific overrides */
  639. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
  640. &frev, &crev, &data_offset)) {
  641. igp_info = (union igp_info *)
  642. (mode_info->atom_context->bios + data_offset);
  643. switch (crev) {
  644. case 6:
  645. switch (id) {
  646. case ASIC_INTERNAL_SS_ON_TMDS:
  647. percentage = le16_to_cpu(igp_info->info_6.usDVISSPercentage);
  648. rate = le16_to_cpu(igp_info->info_6.usDVISSpreadRateIn10Hz);
  649. break;
  650. case ASIC_INTERNAL_SS_ON_HDMI:
  651. percentage = le16_to_cpu(igp_info->info_6.usHDMISSPercentage);
  652. rate = le16_to_cpu(igp_info->info_6.usHDMISSpreadRateIn10Hz);
  653. break;
  654. case ASIC_INTERNAL_SS_ON_LVDS:
  655. percentage = le16_to_cpu(igp_info->info_6.usLvdsSSPercentage);
  656. rate = le16_to_cpu(igp_info->info_6.usLvdsSSpreadRateIn10Hz);
  657. break;
  658. }
  659. break;
  660. case 7:
  661. switch (id) {
  662. case ASIC_INTERNAL_SS_ON_TMDS:
  663. percentage = le16_to_cpu(igp_info->info_7.usDVISSPercentage);
  664. rate = le16_to_cpu(igp_info->info_7.usDVISSpreadRateIn10Hz);
  665. break;
  666. case ASIC_INTERNAL_SS_ON_HDMI:
  667. percentage = le16_to_cpu(igp_info->info_7.usHDMISSPercentage);
  668. rate = le16_to_cpu(igp_info->info_7.usHDMISSpreadRateIn10Hz);
  669. break;
  670. case ASIC_INTERNAL_SS_ON_LVDS:
  671. percentage = le16_to_cpu(igp_info->info_7.usLvdsSSPercentage);
  672. rate = le16_to_cpu(igp_info->info_7.usLvdsSSpreadRateIn10Hz);
  673. break;
  674. }
  675. break;
  676. case 8:
  677. switch (id) {
  678. case ASIC_INTERNAL_SS_ON_TMDS:
  679. percentage = le16_to_cpu(igp_info->info_8.usDVISSPercentage);
  680. rate = le16_to_cpu(igp_info->info_8.usDVISSpreadRateIn10Hz);
  681. break;
  682. case ASIC_INTERNAL_SS_ON_HDMI:
  683. percentage = le16_to_cpu(igp_info->info_8.usHDMISSPercentage);
  684. rate = le16_to_cpu(igp_info->info_8.usHDMISSpreadRateIn10Hz);
  685. break;
  686. case ASIC_INTERNAL_SS_ON_LVDS:
  687. percentage = le16_to_cpu(igp_info->info_8.usLvdsSSPercentage);
  688. rate = le16_to_cpu(igp_info->info_8.usLvdsSSpreadRateIn10Hz);
  689. break;
  690. }
  691. break;
  692. case 9:
  693. switch (id) {
  694. case ASIC_INTERNAL_SS_ON_TMDS:
  695. percentage = le16_to_cpu(igp_info->info_9.usDVISSPercentage);
  696. rate = le16_to_cpu(igp_info->info_9.usDVISSpreadRateIn10Hz);
  697. break;
  698. case ASIC_INTERNAL_SS_ON_HDMI:
  699. percentage = le16_to_cpu(igp_info->info_9.usHDMISSPercentage);
  700. rate = le16_to_cpu(igp_info->info_9.usHDMISSpreadRateIn10Hz);
  701. break;
  702. case ASIC_INTERNAL_SS_ON_LVDS:
  703. percentage = le16_to_cpu(igp_info->info_9.usLvdsSSPercentage);
  704. rate = le16_to_cpu(igp_info->info_9.usLvdsSSpreadRateIn10Hz);
  705. break;
  706. }
  707. break;
  708. default:
  709. DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
  710. break;
  711. }
  712. if (percentage)
  713. ss->percentage = percentage;
  714. if (rate)
  715. ss->rate = rate;
  716. }
  717. }
  718. union asic_ss_info {
  719. struct _ATOM_ASIC_INTERNAL_SS_INFO info;
  720. struct _ATOM_ASIC_INTERNAL_SS_INFO_V2 info_2;
  721. struct _ATOM_ASIC_INTERNAL_SS_INFO_V3 info_3;
  722. };
  723. union asic_ss_assignment {
  724. struct _ATOM_ASIC_SS_ASSIGNMENT v1;
  725. struct _ATOM_ASIC_SS_ASSIGNMENT_V2 v2;
  726. struct _ATOM_ASIC_SS_ASSIGNMENT_V3 v3;
  727. };
  728. bool amdgpu_atombios_get_asic_ss_info(struct amdgpu_device *adev,
  729. struct amdgpu_atom_ss *ss,
  730. int id, u32 clock)
  731. {
  732. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  733. int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
  734. uint16_t data_offset, size;
  735. union asic_ss_info *ss_info;
  736. union asic_ss_assignment *ss_assign;
  737. uint8_t frev, crev;
  738. int i, num_indices;
  739. if (id == ASIC_INTERNAL_MEMORY_SS) {
  740. if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_MEMORY_CLOCK_SS_SUPPORT))
  741. return false;
  742. }
  743. if (id == ASIC_INTERNAL_ENGINE_SS) {
  744. if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_ENGINE_CLOCK_SS_SUPPORT))
  745. return false;
  746. }
  747. memset(ss, 0, sizeof(struct amdgpu_atom_ss));
  748. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
  749. &frev, &crev, &data_offset)) {
  750. ss_info =
  751. (union asic_ss_info *)(mode_info->atom_context->bios + data_offset);
  752. switch (frev) {
  753. case 1:
  754. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  755. sizeof(ATOM_ASIC_SS_ASSIGNMENT);
  756. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info.asSpreadSpectrum[0]);
  757. for (i = 0; i < num_indices; i++) {
  758. if ((ss_assign->v1.ucClockIndication == id) &&
  759. (clock <= le32_to_cpu(ss_assign->v1.ulTargetClockRange))) {
  760. ss->percentage =
  761. le16_to_cpu(ss_assign->v1.usSpreadSpectrumPercentage);
  762. ss->type = ss_assign->v1.ucSpreadSpectrumMode;
  763. ss->rate = le16_to_cpu(ss_assign->v1.usSpreadRateInKhz);
  764. ss->percentage_divider = 100;
  765. return true;
  766. }
  767. ss_assign = (union asic_ss_assignment *)
  768. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT));
  769. }
  770. break;
  771. case 2:
  772. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  773. sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2);
  774. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_2.asSpreadSpectrum[0]);
  775. for (i = 0; i < num_indices; i++) {
  776. if ((ss_assign->v2.ucClockIndication == id) &&
  777. (clock <= le32_to_cpu(ss_assign->v2.ulTargetClockRange))) {
  778. ss->percentage =
  779. le16_to_cpu(ss_assign->v2.usSpreadSpectrumPercentage);
  780. ss->type = ss_assign->v2.ucSpreadSpectrumMode;
  781. ss->rate = le16_to_cpu(ss_assign->v2.usSpreadRateIn10Hz);
  782. ss->percentage_divider = 100;
  783. if ((crev == 2) &&
  784. ((id == ASIC_INTERNAL_ENGINE_SS) ||
  785. (id == ASIC_INTERNAL_MEMORY_SS)))
  786. ss->rate /= 100;
  787. return true;
  788. }
  789. ss_assign = (union asic_ss_assignment *)
  790. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2));
  791. }
  792. break;
  793. case 3:
  794. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  795. sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3);
  796. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_3.asSpreadSpectrum[0]);
  797. for (i = 0; i < num_indices; i++) {
  798. if ((ss_assign->v3.ucClockIndication == id) &&
  799. (clock <= le32_to_cpu(ss_assign->v3.ulTargetClockRange))) {
  800. ss->percentage =
  801. le16_to_cpu(ss_assign->v3.usSpreadSpectrumPercentage);
  802. ss->type = ss_assign->v3.ucSpreadSpectrumMode;
  803. ss->rate = le16_to_cpu(ss_assign->v3.usSpreadRateIn10Hz);
  804. if (ss_assign->v3.ucSpreadSpectrumMode &
  805. SS_MODE_V3_PERCENTAGE_DIV_BY_1000_MASK)
  806. ss->percentage_divider = 1000;
  807. else
  808. ss->percentage_divider = 100;
  809. if ((id == ASIC_INTERNAL_ENGINE_SS) ||
  810. (id == ASIC_INTERNAL_MEMORY_SS))
  811. ss->rate /= 100;
  812. if (adev->flags & AMD_IS_APU)
  813. amdgpu_atombios_get_igp_ss_overrides(adev, ss, id);
  814. return true;
  815. }
  816. ss_assign = (union asic_ss_assignment *)
  817. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3));
  818. }
  819. break;
  820. default:
  821. DRM_ERROR("Unsupported ASIC_InternalSS_Info table: %d %d\n", frev, crev);
  822. break;
  823. }
  824. }
  825. return false;
  826. }
  827. union get_clock_dividers {
  828. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS v1;
  829. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V2 v2;
  830. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V3 v3;
  831. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V4 v4;
  832. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V5 v5;
  833. struct _COMPUTE_GPU_CLOCK_INPUT_PARAMETERS_V1_6 v6_in;
  834. struct _COMPUTE_GPU_CLOCK_OUTPUT_PARAMETERS_V1_6 v6_out;
  835. };
  836. int amdgpu_atombios_get_clock_dividers(struct amdgpu_device *adev,
  837. u8 clock_type,
  838. u32 clock,
  839. bool strobe_mode,
  840. struct atom_clock_dividers *dividers)
  841. {
  842. union get_clock_dividers args;
  843. int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryEnginePLL);
  844. u8 frev, crev;
  845. memset(&args, 0, sizeof(args));
  846. memset(dividers, 0, sizeof(struct atom_clock_dividers));
  847. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  848. return -EINVAL;
  849. switch (crev) {
  850. case 4:
  851. /* fusion */
  852. args.v4.ulClock = cpu_to_le32(clock); /* 10 khz */
  853. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  854. dividers->post_divider = dividers->post_div = args.v4.ucPostDiv;
  855. dividers->real_clock = le32_to_cpu(args.v4.ulClock);
  856. break;
  857. case 6:
  858. /* CI */
  859. /* COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK, COMPUTE_GPUCLK_INPUT_FLAG_SCLK */
  860. args.v6_in.ulClock.ulComputeClockFlag = clock_type;
  861. args.v6_in.ulClock.ulClockFreq = cpu_to_le32(clock); /* 10 khz */
  862. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  863. dividers->whole_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDiv);
  864. dividers->frac_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDivFrac);
  865. dividers->ref_div = args.v6_out.ucPllRefDiv;
  866. dividers->post_div = args.v6_out.ucPllPostDiv;
  867. dividers->flags = args.v6_out.ucPllCntlFlag;
  868. dividers->real_clock = le32_to_cpu(args.v6_out.ulClock.ulClock);
  869. dividers->post_divider = args.v6_out.ulClock.ucPostDiv;
  870. break;
  871. default:
  872. return -EINVAL;
  873. }
  874. return 0;
  875. }
  876. int amdgpu_atombios_get_memory_pll_dividers(struct amdgpu_device *adev,
  877. u32 clock,
  878. bool strobe_mode,
  879. struct atom_mpll_param *mpll_param)
  880. {
  881. COMPUTE_MEMORY_CLOCK_PARAM_PARAMETERS_V2_1 args;
  882. int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryClockParam);
  883. u8 frev, crev;
  884. memset(&args, 0, sizeof(args));
  885. memset(mpll_param, 0, sizeof(struct atom_mpll_param));
  886. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  887. return -EINVAL;
  888. switch (frev) {
  889. case 2:
  890. switch (crev) {
  891. case 1:
  892. /* SI */
  893. args.ulClock = cpu_to_le32(clock); /* 10 khz */
  894. args.ucInputFlag = 0;
  895. if (strobe_mode)
  896. args.ucInputFlag |= MPLL_INPUT_FLAG_STROBE_MODE_EN;
  897. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  898. mpll_param->clkfrac = le16_to_cpu(args.ulFbDiv.usFbDivFrac);
  899. mpll_param->clkf = le16_to_cpu(args.ulFbDiv.usFbDiv);
  900. mpll_param->post_div = args.ucPostDiv;
  901. mpll_param->dll_speed = args.ucDllSpeed;
  902. mpll_param->bwcntl = args.ucBWCntl;
  903. mpll_param->vco_mode =
  904. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_VCO_MODE_MASK);
  905. mpll_param->yclk_sel =
  906. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_BYPASS_DQ_PLL) ? 1 : 0;
  907. mpll_param->qdr =
  908. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_QDR_ENABLE) ? 1 : 0;
  909. mpll_param->half_rate =
  910. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_AD_HALF_RATE) ? 1 : 0;
  911. break;
  912. default:
  913. return -EINVAL;
  914. }
  915. break;
  916. default:
  917. return -EINVAL;
  918. }
  919. return 0;
  920. }
  921. uint32_t amdgpu_atombios_get_engine_clock(struct amdgpu_device *adev)
  922. {
  923. GET_ENGINE_CLOCK_PS_ALLOCATION args;
  924. int index = GetIndexIntoMasterTable(COMMAND, GetEngineClock);
  925. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  926. return le32_to_cpu(args.ulReturnEngineClock);
  927. }
  928. uint32_t amdgpu_atombios_get_memory_clock(struct amdgpu_device *adev)
  929. {
  930. GET_MEMORY_CLOCK_PS_ALLOCATION args;
  931. int index = GetIndexIntoMasterTable(COMMAND, GetMemoryClock);
  932. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  933. return le32_to_cpu(args.ulReturnMemoryClock);
  934. }
  935. void amdgpu_atombios_set_engine_clock(struct amdgpu_device *adev,
  936. uint32_t eng_clock)
  937. {
  938. SET_ENGINE_CLOCK_PS_ALLOCATION args;
  939. int index = GetIndexIntoMasterTable(COMMAND, SetEngineClock);
  940. args.ulTargetEngineClock = cpu_to_le32(eng_clock); /* 10 khz */
  941. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  942. }
  943. void amdgpu_atombios_set_memory_clock(struct amdgpu_device *adev,
  944. uint32_t mem_clock)
  945. {
  946. SET_MEMORY_CLOCK_PS_ALLOCATION args;
  947. int index = GetIndexIntoMasterTable(COMMAND, SetMemoryClock);
  948. if (adev->flags & AMD_IS_APU)
  949. return;
  950. args.ulTargetMemoryClock = cpu_to_le32(mem_clock); /* 10 khz */
  951. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  952. }
  953. void amdgpu_atombios_set_engine_dram_timings(struct amdgpu_device *adev,
  954. u32 eng_clock, u32 mem_clock)
  955. {
  956. SET_ENGINE_CLOCK_PS_ALLOCATION args;
  957. int index = GetIndexIntoMasterTable(COMMAND, DynamicMemorySettings);
  958. u32 tmp;
  959. memset(&args, 0, sizeof(args));
  960. tmp = eng_clock & SET_CLOCK_FREQ_MASK;
  961. tmp |= (COMPUTE_ENGINE_PLL_PARAM << 24);
  962. args.ulTargetEngineClock = cpu_to_le32(tmp);
  963. if (mem_clock)
  964. args.sReserved.ulClock = cpu_to_le32(mem_clock & SET_CLOCK_FREQ_MASK);
  965. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  966. }
  967. union set_voltage {
  968. struct _SET_VOLTAGE_PS_ALLOCATION alloc;
  969. struct _SET_VOLTAGE_PARAMETERS v1;
  970. struct _SET_VOLTAGE_PARAMETERS_V2 v2;
  971. struct _SET_VOLTAGE_PARAMETERS_V1_3 v3;
  972. };
  973. void amdgpu_atombios_set_voltage(struct amdgpu_device *adev,
  974. u16 voltage_level,
  975. u8 voltage_type)
  976. {
  977. union set_voltage args;
  978. int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
  979. u8 frev, crev, volt_index = voltage_level;
  980. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  981. return;
  982. /* 0xff01 is a flag rather then an actual voltage */
  983. if (voltage_level == 0xff01)
  984. return;
  985. switch (crev) {
  986. case 1:
  987. args.v1.ucVoltageType = voltage_type;
  988. args.v1.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_ALL_SOURCE;
  989. args.v1.ucVoltageIndex = volt_index;
  990. break;
  991. case 2:
  992. args.v2.ucVoltageType = voltage_type;
  993. args.v2.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_SET_VOLTAGE;
  994. args.v2.usVoltageLevel = cpu_to_le16(voltage_level);
  995. break;
  996. case 3:
  997. args.v3.ucVoltageType = voltage_type;
  998. args.v3.ucVoltageMode = ATOM_SET_VOLTAGE;
  999. args.v3.usVoltageLevel = cpu_to_le16(voltage_level);
  1000. break;
  1001. default:
  1002. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1003. return;
  1004. }
  1005. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1006. }
  1007. int amdgpu_atombios_get_leakage_id_from_vbios(struct amdgpu_device *adev,
  1008. u16 *leakage_id)
  1009. {
  1010. union set_voltage args;
  1011. int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
  1012. u8 frev, crev;
  1013. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  1014. return -EINVAL;
  1015. switch (crev) {
  1016. case 3:
  1017. case 4:
  1018. args.v3.ucVoltageType = 0;
  1019. args.v3.ucVoltageMode = ATOM_GET_LEAKAGE_ID;
  1020. args.v3.usVoltageLevel = 0;
  1021. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1022. *leakage_id = le16_to_cpu(args.v3.usVoltageLevel);
  1023. break;
  1024. default:
  1025. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1026. return -EINVAL;
  1027. }
  1028. return 0;
  1029. }
  1030. int amdgpu_atombios_get_leakage_vddc_based_on_leakage_params(struct amdgpu_device *adev,
  1031. u16 *vddc, u16 *vddci,
  1032. u16 virtual_voltage_id,
  1033. u16 vbios_voltage_id)
  1034. {
  1035. int index = GetIndexIntoMasterTable(DATA, ASIC_ProfilingInfo);
  1036. u8 frev, crev;
  1037. u16 data_offset, size;
  1038. int i, j;
  1039. ATOM_ASIC_PROFILING_INFO_V2_1 *profile;
  1040. u16 *leakage_bin, *vddc_id_buf, *vddc_buf, *vddci_id_buf, *vddci_buf;
  1041. *vddc = 0;
  1042. *vddci = 0;
  1043. if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1044. &frev, &crev, &data_offset))
  1045. return -EINVAL;
  1046. profile = (ATOM_ASIC_PROFILING_INFO_V2_1 *)
  1047. (adev->mode_info.atom_context->bios + data_offset);
  1048. switch (frev) {
  1049. case 1:
  1050. return -EINVAL;
  1051. case 2:
  1052. switch (crev) {
  1053. case 1:
  1054. if (size < sizeof(ATOM_ASIC_PROFILING_INFO_V2_1))
  1055. return -EINVAL;
  1056. leakage_bin = (u16 *)
  1057. (adev->mode_info.atom_context->bios + data_offset +
  1058. le16_to_cpu(profile->usLeakageBinArrayOffset));
  1059. vddc_id_buf = (u16 *)
  1060. (adev->mode_info.atom_context->bios + data_offset +
  1061. le16_to_cpu(profile->usElbVDDC_IdArrayOffset));
  1062. vddc_buf = (u16 *)
  1063. (adev->mode_info.atom_context->bios + data_offset +
  1064. le16_to_cpu(profile->usElbVDDC_LevelArrayOffset));
  1065. vddci_id_buf = (u16 *)
  1066. (adev->mode_info.atom_context->bios + data_offset +
  1067. le16_to_cpu(profile->usElbVDDCI_IdArrayOffset));
  1068. vddci_buf = (u16 *)
  1069. (adev->mode_info.atom_context->bios + data_offset +
  1070. le16_to_cpu(profile->usElbVDDCI_LevelArrayOffset));
  1071. if (profile->ucElbVDDC_Num > 0) {
  1072. for (i = 0; i < profile->ucElbVDDC_Num; i++) {
  1073. if (vddc_id_buf[i] == virtual_voltage_id) {
  1074. for (j = 0; j < profile->ucLeakageBinNum; j++) {
  1075. if (vbios_voltage_id <= leakage_bin[j]) {
  1076. *vddc = vddc_buf[j * profile->ucElbVDDC_Num + i];
  1077. break;
  1078. }
  1079. }
  1080. break;
  1081. }
  1082. }
  1083. }
  1084. if (profile->ucElbVDDCI_Num > 0) {
  1085. for (i = 0; i < profile->ucElbVDDCI_Num; i++) {
  1086. if (vddci_id_buf[i] == virtual_voltage_id) {
  1087. for (j = 0; j < profile->ucLeakageBinNum; j++) {
  1088. if (vbios_voltage_id <= leakage_bin[j]) {
  1089. *vddci = vddci_buf[j * profile->ucElbVDDCI_Num + i];
  1090. break;
  1091. }
  1092. }
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. break;
  1098. default:
  1099. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1100. return -EINVAL;
  1101. }
  1102. break;
  1103. default:
  1104. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1105. return -EINVAL;
  1106. }
  1107. return 0;
  1108. }
  1109. union get_voltage_info {
  1110. struct _GET_VOLTAGE_INFO_INPUT_PARAMETER_V1_2 in;
  1111. struct _GET_EVV_VOLTAGE_INFO_OUTPUT_PARAMETER_V1_2 evv_out;
  1112. };
  1113. int amdgpu_atombios_get_voltage_evv(struct amdgpu_device *adev,
  1114. u16 virtual_voltage_id,
  1115. u16 *voltage)
  1116. {
  1117. int index = GetIndexIntoMasterTable(COMMAND, GetVoltageInfo);
  1118. u32 entry_id;
  1119. u32 count = adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count;
  1120. union get_voltage_info args;
  1121. for (entry_id = 0; entry_id < count; entry_id++) {
  1122. if (adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].v ==
  1123. virtual_voltage_id)
  1124. break;
  1125. }
  1126. if (entry_id >= count)
  1127. return -EINVAL;
  1128. args.in.ucVoltageType = VOLTAGE_TYPE_VDDC;
  1129. args.in.ucVoltageMode = ATOM_GET_VOLTAGE_EVV_VOLTAGE;
  1130. args.in.usVoltageLevel = cpu_to_le16(virtual_voltage_id);
  1131. args.in.ulSCLKFreq =
  1132. cpu_to_le32(adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].clk);
  1133. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1134. *voltage = le16_to_cpu(args.evv_out.usVoltageLevel);
  1135. return 0;
  1136. }
  1137. union voltage_object_info {
  1138. struct _ATOM_VOLTAGE_OBJECT_INFO v1;
  1139. struct _ATOM_VOLTAGE_OBJECT_INFO_V2 v2;
  1140. struct _ATOM_VOLTAGE_OBJECT_INFO_V3_1 v3;
  1141. };
  1142. union voltage_object {
  1143. struct _ATOM_VOLTAGE_OBJECT v1;
  1144. struct _ATOM_VOLTAGE_OBJECT_V2 v2;
  1145. union _ATOM_VOLTAGE_OBJECT_V3 v3;
  1146. };
  1147. static ATOM_VOLTAGE_OBJECT_V3 *amdgpu_atombios_lookup_voltage_object_v3(ATOM_VOLTAGE_OBJECT_INFO_V3_1 *v3,
  1148. u8 voltage_type, u8 voltage_mode)
  1149. {
  1150. u32 size = le16_to_cpu(v3->sHeader.usStructureSize);
  1151. u32 offset = offsetof(ATOM_VOLTAGE_OBJECT_INFO_V3_1, asVoltageObj[0]);
  1152. u8 *start = (u8*)v3;
  1153. while (offset < size) {
  1154. ATOM_VOLTAGE_OBJECT_V3 *vo = (ATOM_VOLTAGE_OBJECT_V3 *)(start + offset);
  1155. if ((vo->asGpioVoltageObj.sHeader.ucVoltageType == voltage_type) &&
  1156. (vo->asGpioVoltageObj.sHeader.ucVoltageMode == voltage_mode))
  1157. return vo;
  1158. offset += le16_to_cpu(vo->asGpioVoltageObj.sHeader.usSize);
  1159. }
  1160. return NULL;
  1161. }
  1162. bool
  1163. amdgpu_atombios_is_voltage_gpio(struct amdgpu_device *adev,
  1164. u8 voltage_type, u8 voltage_mode)
  1165. {
  1166. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1167. u8 frev, crev;
  1168. u16 data_offset, size;
  1169. union voltage_object_info *voltage_info;
  1170. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1171. &frev, &crev, &data_offset)) {
  1172. voltage_info = (union voltage_object_info *)
  1173. (adev->mode_info.atom_context->bios + data_offset);
  1174. switch (frev) {
  1175. case 3:
  1176. switch (crev) {
  1177. case 1:
  1178. if (amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1179. voltage_type, voltage_mode))
  1180. return true;
  1181. break;
  1182. default:
  1183. DRM_ERROR("unknown voltage object table\n");
  1184. return false;
  1185. }
  1186. break;
  1187. default:
  1188. DRM_ERROR("unknown voltage object table\n");
  1189. return false;
  1190. }
  1191. }
  1192. return false;
  1193. }
  1194. int amdgpu_atombios_get_voltage_table(struct amdgpu_device *adev,
  1195. u8 voltage_type, u8 voltage_mode,
  1196. struct atom_voltage_table *voltage_table)
  1197. {
  1198. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1199. u8 frev, crev;
  1200. u16 data_offset, size;
  1201. int i;
  1202. union voltage_object_info *voltage_info;
  1203. union voltage_object *voltage_object = NULL;
  1204. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1205. &frev, &crev, &data_offset)) {
  1206. voltage_info = (union voltage_object_info *)
  1207. (adev->mode_info.atom_context->bios + data_offset);
  1208. switch (frev) {
  1209. case 3:
  1210. switch (crev) {
  1211. case 1:
  1212. voltage_object = (union voltage_object *)
  1213. amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1214. voltage_type, voltage_mode);
  1215. if (voltage_object) {
  1216. ATOM_GPIO_VOLTAGE_OBJECT_V3 *gpio =
  1217. &voltage_object->v3.asGpioVoltageObj;
  1218. VOLTAGE_LUT_ENTRY_V2 *lut;
  1219. if (gpio->ucGpioEntryNum > MAX_VOLTAGE_ENTRIES)
  1220. return -EINVAL;
  1221. lut = &gpio->asVolGpioLut[0];
  1222. for (i = 0; i < gpio->ucGpioEntryNum; i++) {
  1223. voltage_table->entries[i].value =
  1224. le16_to_cpu(lut->usVoltageValue);
  1225. voltage_table->entries[i].smio_low =
  1226. le32_to_cpu(lut->ulVoltageId);
  1227. lut = (VOLTAGE_LUT_ENTRY_V2 *)
  1228. ((u8 *)lut + sizeof(VOLTAGE_LUT_ENTRY_V2));
  1229. }
  1230. voltage_table->mask_low = le32_to_cpu(gpio->ulGpioMaskVal);
  1231. voltage_table->count = gpio->ucGpioEntryNum;
  1232. voltage_table->phase_delay = gpio->ucPhaseDelay;
  1233. return 0;
  1234. }
  1235. break;
  1236. default:
  1237. DRM_ERROR("unknown voltage object table\n");
  1238. return -EINVAL;
  1239. }
  1240. break;
  1241. default:
  1242. DRM_ERROR("unknown voltage object table\n");
  1243. return -EINVAL;
  1244. }
  1245. }
  1246. return -EINVAL;
  1247. }
  1248. union vram_info {
  1249. struct _ATOM_VRAM_INFO_V3 v1_3;
  1250. struct _ATOM_VRAM_INFO_V4 v1_4;
  1251. struct _ATOM_VRAM_INFO_HEADER_V2_1 v2_1;
  1252. };
  1253. #define MEM_ID_MASK 0xff000000
  1254. #define MEM_ID_SHIFT 24
  1255. #define CLOCK_RANGE_MASK 0x00ffffff
  1256. #define CLOCK_RANGE_SHIFT 0
  1257. #define LOW_NIBBLE_MASK 0xf
  1258. #define DATA_EQU_PREV 0
  1259. #define DATA_FROM_TABLE 4
  1260. int amdgpu_atombios_init_mc_reg_table(struct amdgpu_device *adev,
  1261. u8 module_index,
  1262. struct atom_mc_reg_table *reg_table)
  1263. {
  1264. int index = GetIndexIntoMasterTable(DATA, VRAM_Info);
  1265. u8 frev, crev, num_entries, t_mem_id, num_ranges = 0;
  1266. u32 i = 0, j;
  1267. u16 data_offset, size;
  1268. union vram_info *vram_info;
  1269. memset(reg_table, 0, sizeof(struct atom_mc_reg_table));
  1270. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1271. &frev, &crev, &data_offset)) {
  1272. vram_info = (union vram_info *)
  1273. (adev->mode_info.atom_context->bios + data_offset);
  1274. switch (frev) {
  1275. case 1:
  1276. DRM_ERROR("old table version %d, %d\n", frev, crev);
  1277. return -EINVAL;
  1278. case 2:
  1279. switch (crev) {
  1280. case 1:
  1281. if (module_index < vram_info->v2_1.ucNumOfVRAMModule) {
  1282. ATOM_INIT_REG_BLOCK *reg_block =
  1283. (ATOM_INIT_REG_BLOCK *)
  1284. ((u8 *)vram_info + le16_to_cpu(vram_info->v2_1.usMemClkPatchTblOffset));
  1285. ATOM_MEMORY_SETTING_DATA_BLOCK *reg_data =
  1286. (ATOM_MEMORY_SETTING_DATA_BLOCK *)
  1287. ((u8 *)reg_block + (2 * sizeof(u16)) +
  1288. le16_to_cpu(reg_block->usRegIndexTblSize));
  1289. ATOM_INIT_REG_INDEX_FORMAT *format = &reg_block->asRegIndexBuf[0];
  1290. num_entries = (u8)((le16_to_cpu(reg_block->usRegIndexTblSize)) /
  1291. sizeof(ATOM_INIT_REG_INDEX_FORMAT)) - 1;
  1292. if (num_entries > VBIOS_MC_REGISTER_ARRAY_SIZE)
  1293. return -EINVAL;
  1294. while (i < num_entries) {
  1295. if (format->ucPreRegDataLength & ACCESS_PLACEHOLDER)
  1296. break;
  1297. reg_table->mc_reg_address[i].s1 =
  1298. (u16)(le16_to_cpu(format->usRegIndex));
  1299. reg_table->mc_reg_address[i].pre_reg_data =
  1300. (u8)(format->ucPreRegDataLength);
  1301. i++;
  1302. format = (ATOM_INIT_REG_INDEX_FORMAT *)
  1303. ((u8 *)format + sizeof(ATOM_INIT_REG_INDEX_FORMAT));
  1304. }
  1305. reg_table->last = i;
  1306. while ((le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK) &&
  1307. (num_ranges < VBIOS_MAX_AC_TIMING_ENTRIES)) {
  1308. t_mem_id = (u8)((le32_to_cpu(*(u32 *)reg_data) & MEM_ID_MASK)
  1309. >> MEM_ID_SHIFT);
  1310. if (module_index == t_mem_id) {
  1311. reg_table->mc_reg_table_entry[num_ranges].mclk_max =
  1312. (u32)((le32_to_cpu(*(u32 *)reg_data) & CLOCK_RANGE_MASK)
  1313. >> CLOCK_RANGE_SHIFT);
  1314. for (i = 0, j = 1; i < reg_table->last; i++) {
  1315. if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_FROM_TABLE) {
  1316. reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
  1317. (u32)le32_to_cpu(*((u32 *)reg_data + j));
  1318. j++;
  1319. } else if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_EQU_PREV) {
  1320. reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
  1321. reg_table->mc_reg_table_entry[num_ranges].mc_data[i - 1];
  1322. }
  1323. }
  1324. num_ranges++;
  1325. }
  1326. reg_data = (ATOM_MEMORY_SETTING_DATA_BLOCK *)
  1327. ((u8 *)reg_data + le16_to_cpu(reg_block->usRegDataBlkSize));
  1328. }
  1329. if (le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK)
  1330. return -EINVAL;
  1331. reg_table->num_entries = num_ranges;
  1332. } else
  1333. return -EINVAL;
  1334. break;
  1335. default:
  1336. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1337. return -EINVAL;
  1338. }
  1339. break;
  1340. default:
  1341. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1342. return -EINVAL;
  1343. }
  1344. return 0;
  1345. }
  1346. return -EINVAL;
  1347. }
  1348. void amdgpu_atombios_scratch_regs_lock(struct amdgpu_device *adev, bool lock)
  1349. {
  1350. uint32_t bios_6_scratch;
  1351. bios_6_scratch = RREG32(mmBIOS_SCRATCH_6);
  1352. if (lock) {
  1353. bios_6_scratch |= ATOM_S6_CRITICAL_STATE;
  1354. bios_6_scratch &= ~ATOM_S6_ACC_MODE;
  1355. } else {
  1356. bios_6_scratch &= ~ATOM_S6_CRITICAL_STATE;
  1357. bios_6_scratch |= ATOM_S6_ACC_MODE;
  1358. }
  1359. WREG32(mmBIOS_SCRATCH_6, bios_6_scratch);
  1360. }
  1361. void amdgpu_atombios_scratch_regs_init(struct amdgpu_device *adev)
  1362. {
  1363. uint32_t bios_2_scratch, bios_6_scratch;
  1364. bios_2_scratch = RREG32(mmBIOS_SCRATCH_2);
  1365. bios_6_scratch = RREG32(mmBIOS_SCRATCH_6);
  1366. /* let the bios control the backlight */
  1367. bios_2_scratch &= ~ATOM_S2_VRI_BRIGHT_ENABLE;
  1368. /* tell the bios not to handle mode switching */
  1369. bios_6_scratch |= ATOM_S6_ACC_BLOCK_DISPLAY_SWITCH;
  1370. /* clear the vbios dpms state */
  1371. bios_2_scratch &= ~ATOM_S2_DEVICE_DPMS_STATE;
  1372. WREG32(mmBIOS_SCRATCH_2, bios_2_scratch);
  1373. WREG32(mmBIOS_SCRATCH_6, bios_6_scratch);
  1374. }
  1375. void amdgpu_atombios_scratch_regs_save(struct amdgpu_device *adev)
  1376. {
  1377. int i;
  1378. for (i = 0; i < AMDGPU_BIOS_NUM_SCRATCH; i++)
  1379. adev->bios_scratch[i] = RREG32(mmBIOS_SCRATCH_0 + i);
  1380. }
  1381. void amdgpu_atombios_scratch_regs_restore(struct amdgpu_device *adev)
  1382. {
  1383. int i;
  1384. for (i = 0; i < AMDGPU_BIOS_NUM_SCRATCH; i++)
  1385. WREG32(mmBIOS_SCRATCH_0 + i, adev->bios_scratch[i]);
  1386. }
  1387. /* Atom needs data in little endian format so swap as appropriate when copying
  1388. * data to or from atom. Note that atom operates on dw units.
  1389. *
  1390. * Use to_le=true when sending data to atom and provide at least
  1391. * ALIGN(num_bytes,4) bytes in the dst buffer.
  1392. *
  1393. * Use to_le=false when receiving data from atom and provide ALIGN(num_bytes,4)
  1394. * byes in the src buffer.
  1395. */
  1396. void amdgpu_atombios_copy_swap(u8 *dst, u8 *src, u8 num_bytes, bool to_le)
  1397. {
  1398. #ifdef __BIG_ENDIAN
  1399. u32 src_tmp[5], dst_tmp[5];
  1400. int i;
  1401. u8 align_num_bytes = ALIGN(num_bytes, 4);
  1402. if (to_le) {
  1403. memcpy(src_tmp, src, num_bytes);
  1404. for (i = 0; i < align_num_bytes / 4; i++)
  1405. dst_tmp[i] = cpu_to_le32(src_tmp[i]);
  1406. memcpy(dst, dst_tmp, align_num_bytes);
  1407. } else {
  1408. memcpy(src_tmp, src, align_num_bytes);
  1409. for (i = 0; i < align_num_bytes / 4; i++)
  1410. dst_tmp[i] = le32_to_cpu(src_tmp[i]);
  1411. memcpy(dst, dst_tmp, num_bytes);
  1412. }
  1413. #else
  1414. memcpy(dst, src, num_bytes);
  1415. #endif
  1416. }