cppc_acpi.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers.
  4. *
  5. * (C) Copyright 2014, 2015 Linaro Ltd.
  6. * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
  7. *
  8. * CPPC describes a few methods for controlling CPU performance using
  9. * information from a per CPU table called CPC. This table is described in
  10. * the ACPI v5.0+ specification. The table consists of a list of
  11. * registers which may be memory mapped or hardware registers and also may
  12. * include some static integer values.
  13. *
  14. * CPU performance is on an abstract continuous scale as against a discretized
  15. * P-state scale which is tied to CPU frequency only. In brief, the basic
  16. * operation involves:
  17. *
  18. * - OS makes a CPU performance request. (Can provide min and max bounds)
  19. *
  20. * - Platform (such as BMC) is free to optimize request within requested bounds
  21. * depending on power/thermal budgets etc.
  22. *
  23. * - Platform conveys its decision back to OS
  24. *
  25. * The communication between OS and platform occurs through another medium
  26. * called (PCC) Platform Communication Channel. This is a generic mailbox like
  27. * mechanism which includes doorbell semantics to indicate register updates.
  28. * See drivers/mailbox/pcc.c for details on PCC.
  29. *
  30. * Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and
  31. * above specifications.
  32. */
  33. #define pr_fmt(fmt) "ACPI CPPC: " fmt
  34. #include <linux/delay.h>
  35. #include <linux/iopoll.h>
  36. #include <linux/ktime.h>
  37. #include <linux/rwsem.h>
  38. #include <linux/wait.h>
  39. #include <linux/topology.h>
  40. #include <linux/dmi.h>
  41. #include <linux/units.h>
  42. #include <linux/unaligned.h>
  43. #include <acpi/cppc_acpi.h>
  44. struct cppc_pcc_data {
  45. struct pcc_mbox_chan *pcc_channel;
  46. bool pcc_channel_acquired;
  47. unsigned int deadline_us;
  48. unsigned int pcc_mpar, pcc_mrtt, pcc_nominal;
  49. bool pending_pcc_write_cmd; /* Any pending/batched PCC write cmds? */
  50. bool platform_owns_pcc; /* Ownership of PCC subspace */
  51. unsigned int pcc_write_cnt; /* Running count of PCC write commands */
  52. /*
  53. * Lock to provide controlled access to the PCC channel.
  54. *
  55. * For performance critical usecases(currently cppc_set_perf)
  56. * We need to take read_lock and check if channel belongs to OSPM
  57. * before reading or writing to PCC subspace
  58. * We need to take write_lock before transferring the channel
  59. * ownership to the platform via a Doorbell
  60. * This allows us to batch a number of CPPC requests if they happen
  61. * to originate in about the same time
  62. *
  63. * For non-performance critical usecases(init)
  64. * Take write_lock for all purposes which gives exclusive access
  65. */
  66. struct rw_semaphore pcc_lock;
  67. /* Wait queue for CPUs whose requests were batched */
  68. wait_queue_head_t pcc_write_wait_q;
  69. ktime_t last_cmd_cmpl_time;
  70. ktime_t last_mpar_reset;
  71. int mpar_count;
  72. int refcount;
  73. };
  74. /* Array to represent the PCC channel per subspace ID */
  75. static struct cppc_pcc_data *pcc_data[MAX_PCC_SUBSPACES];
  76. /* The cpu_pcc_subspace_idx contains per CPU subspace ID */
  77. static DEFINE_PER_CPU(int, cpu_pcc_subspace_idx);
  78. /*
  79. * The cpc_desc structure contains the ACPI register details
  80. * as described in the per CPU _CPC tables. The details
  81. * include the type of register (e.g. PCC, System IO, FFH etc.)
  82. * and destination addresses which lets us READ/WRITE CPU performance
  83. * information using the appropriate I/O methods.
  84. */
  85. static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr);
  86. /* pcc mapped address + header size + offset within PCC subspace */
  87. #define GET_PCC_VADDR(offs, pcc_ss_id) (pcc_data[pcc_ss_id]->pcc_channel->shmem + \
  88. 0x8 + (offs))
  89. /* Check if a CPC register is in PCC */
  90. #define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
  91. (cpc)->cpc_entry.reg.space_id == \
  92. ACPI_ADR_SPACE_PLATFORM_COMM)
  93. /* Check if a CPC register is in FFH */
  94. #define CPC_IN_FFH(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
  95. (cpc)->cpc_entry.reg.space_id == \
  96. ACPI_ADR_SPACE_FIXED_HARDWARE)
  97. /* Check if a CPC register is in SystemMemory */
  98. #define CPC_IN_SYSTEM_MEMORY(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
  99. (cpc)->cpc_entry.reg.space_id == \
  100. ACPI_ADR_SPACE_SYSTEM_MEMORY)
  101. /* Check if a CPC register is in SystemIo */
  102. #define CPC_IN_SYSTEM_IO(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
  103. (cpc)->cpc_entry.reg.space_id == \
  104. ACPI_ADR_SPACE_SYSTEM_IO)
  105. /* Evaluates to True if reg is a NULL register descriptor */
  106. #define IS_NULL_REG(reg) ((reg)->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY && \
  107. (reg)->address == 0 && \
  108. (reg)->bit_width == 0 && \
  109. (reg)->bit_offset == 0 && \
  110. (reg)->access_width == 0)
  111. /* Evaluates to True if an optional cpc field is supported */
  112. #define CPC_SUPPORTED(cpc) ((cpc)->type == ACPI_TYPE_INTEGER ? \
  113. !!(cpc)->cpc_entry.int_value : \
  114. !IS_NULL_REG(&(cpc)->cpc_entry.reg))
  115. /*
  116. * Each bit indicates the optionality of the register in per-cpu
  117. * cpc_regs[] with the corresponding index. 0 means mandatory and 1
  118. * means optional.
  119. */
  120. #define REG_OPTIONAL (0x1FC7D0)
  121. /*
  122. * Use the index of the register in per-cpu cpc_regs[] to check if
  123. * it's an optional one.
  124. */
  125. #define IS_OPTIONAL_CPC_REG(reg_idx) (REG_OPTIONAL & (1U << (reg_idx)))
  126. /*
  127. * Arbitrary Retries in case the remote processor is slow to respond
  128. * to PCC commands. Keeping it high enough to cover emulators where
  129. * the processors run painfully slow.
  130. */
  131. #define NUM_RETRIES 500ULL
  132. #define OVER_16BTS_MASK ~0xFFFFULL
  133. #define define_one_cppc_ro(_name) \
  134. static struct kobj_attribute _name = \
  135. __ATTR(_name, 0444, show_##_name, NULL)
  136. #define to_cpc_desc(a) container_of(a, struct cpc_desc, kobj)
  137. #define show_cppc_data(access_fn, struct_name, member_name) \
  138. static ssize_t show_##member_name(struct kobject *kobj, \
  139. struct kobj_attribute *attr, char *buf) \
  140. { \
  141. struct cpc_desc *cpc_ptr = to_cpc_desc(kobj); \
  142. struct struct_name st_name = {0}; \
  143. int ret; \
  144. \
  145. ret = access_fn(cpc_ptr->cpu_id, &st_name); \
  146. if (ret) \
  147. return ret; \
  148. \
  149. return sysfs_emit(buf, "%llu\n", \
  150. (u64)st_name.member_name); \
  151. } \
  152. define_one_cppc_ro(member_name)
  153. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, highest_perf);
  154. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_perf);
  155. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_perf);
  156. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_nonlinear_perf);
  157. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, guaranteed_perf);
  158. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_freq);
  159. show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_freq);
  160. show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, reference_perf);
  161. show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, wraparound_time);
  162. /* Check for valid access_width, otherwise, fallback to using bit_width */
  163. #define GET_BIT_WIDTH(reg) ((reg)->access_width ? (8 << ((reg)->access_width - 1)) : (reg)->bit_width)
  164. /* Shift and apply the mask for CPC reads/writes */
  165. #define MASK_VAL_READ(reg, val) (((val) >> (reg)->bit_offset) & \
  166. GENMASK(((reg)->bit_width) - 1, 0))
  167. #define MASK_VAL_WRITE(reg, prev_val, val) \
  168. ((((val) & GENMASK(((reg)->bit_width) - 1, 0)) << (reg)->bit_offset) | \
  169. ((prev_val) & ~(GENMASK(((reg)->bit_width) - 1, 0) << (reg)->bit_offset))) \
  170. static ssize_t show_feedback_ctrs(struct kobject *kobj,
  171. struct kobj_attribute *attr, char *buf)
  172. {
  173. struct cpc_desc *cpc_ptr = to_cpc_desc(kobj);
  174. struct cppc_perf_fb_ctrs fb_ctrs = {0};
  175. int ret;
  176. ret = cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs);
  177. if (ret)
  178. return ret;
  179. return sysfs_emit(buf, "ref:%llu del:%llu\n",
  180. fb_ctrs.reference, fb_ctrs.delivered);
  181. }
  182. define_one_cppc_ro(feedback_ctrs);
  183. static struct attribute *cppc_attrs[] = {
  184. &feedback_ctrs.attr,
  185. &reference_perf.attr,
  186. &wraparound_time.attr,
  187. &highest_perf.attr,
  188. &lowest_perf.attr,
  189. &lowest_nonlinear_perf.attr,
  190. &guaranteed_perf.attr,
  191. &nominal_perf.attr,
  192. &nominal_freq.attr,
  193. &lowest_freq.attr,
  194. NULL
  195. };
  196. ATTRIBUTE_GROUPS(cppc);
  197. static const struct kobj_type cppc_ktype = {
  198. .sysfs_ops = &kobj_sysfs_ops,
  199. .default_groups = cppc_groups,
  200. };
  201. static int check_pcc_chan(int pcc_ss_id, bool chk_err_bit)
  202. {
  203. int ret, status;
  204. struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
  205. struct acpi_pcct_shared_memory __iomem *generic_comm_base =
  206. pcc_ss_data->pcc_channel->shmem;
  207. if (!pcc_ss_data->platform_owns_pcc)
  208. return 0;
  209. /*
  210. * Poll PCC status register every 3us(delay_us) for maximum of
  211. * deadline_us(timeout_us) until PCC command complete bit is set(cond)
  212. */
  213. ret = readw_relaxed_poll_timeout(&generic_comm_base->status, status,
  214. status & PCC_CMD_COMPLETE_MASK, 3,
  215. pcc_ss_data->deadline_us);
  216. if (likely(!ret)) {
  217. pcc_ss_data->platform_owns_pcc = false;
  218. if (chk_err_bit && (status & PCC_ERROR_MASK))
  219. ret = -EIO;
  220. }
  221. if (unlikely(ret))
  222. pr_err("PCC check channel failed for ss: %d. ret=%d\n",
  223. pcc_ss_id, ret);
  224. return ret;
  225. }
  226. /*
  227. * This function transfers the ownership of the PCC to the platform
  228. * So it must be called while holding write_lock(pcc_lock)
  229. */
  230. static int send_pcc_cmd(int pcc_ss_id, u16 cmd)
  231. {
  232. int ret = -EIO, i;
  233. struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
  234. struct acpi_pcct_shared_memory __iomem *generic_comm_base =
  235. pcc_ss_data->pcc_channel->shmem;
  236. unsigned int time_delta;
  237. /*
  238. * For CMD_WRITE we know for a fact the caller should have checked
  239. * the channel before writing to PCC space
  240. */
  241. if (cmd == CMD_READ) {
  242. /*
  243. * If there are pending cpc_writes, then we stole the channel
  244. * before write completion, so first send a WRITE command to
  245. * platform
  246. */
  247. if (pcc_ss_data->pending_pcc_write_cmd)
  248. send_pcc_cmd(pcc_ss_id, CMD_WRITE);
  249. ret = check_pcc_chan(pcc_ss_id, false);
  250. if (ret)
  251. goto end;
  252. } else /* CMD_WRITE */
  253. pcc_ss_data->pending_pcc_write_cmd = FALSE;
  254. /*
  255. * Handle the Minimum Request Turnaround Time(MRTT)
  256. * "The minimum amount of time that OSPM must wait after the completion
  257. * of a command before issuing the next command, in microseconds"
  258. */
  259. if (pcc_ss_data->pcc_mrtt) {
  260. time_delta = ktime_us_delta(ktime_get(),
  261. pcc_ss_data->last_cmd_cmpl_time);
  262. if (pcc_ss_data->pcc_mrtt > time_delta)
  263. udelay(pcc_ss_data->pcc_mrtt - time_delta);
  264. }
  265. /*
  266. * Handle the non-zero Maximum Periodic Access Rate(MPAR)
  267. * "The maximum number of periodic requests that the subspace channel can
  268. * support, reported in commands per minute. 0 indicates no limitation."
  269. *
  270. * This parameter should be ideally zero or large enough so that it can
  271. * handle maximum number of requests that all the cores in the system can
  272. * collectively generate. If it is not, we will follow the spec and just
  273. * not send the request to the platform after hitting the MPAR limit in
  274. * any 60s window
  275. */
  276. if (pcc_ss_data->pcc_mpar) {
  277. if (pcc_ss_data->mpar_count == 0) {
  278. time_delta = ktime_ms_delta(ktime_get(),
  279. pcc_ss_data->last_mpar_reset);
  280. if ((time_delta < 60 * MSEC_PER_SEC) && pcc_ss_data->last_mpar_reset) {
  281. pr_debug("PCC cmd for subspace %d not sent due to MPAR limit",
  282. pcc_ss_id);
  283. ret = -EIO;
  284. goto end;
  285. }
  286. pcc_ss_data->last_mpar_reset = ktime_get();
  287. pcc_ss_data->mpar_count = pcc_ss_data->pcc_mpar;
  288. }
  289. pcc_ss_data->mpar_count--;
  290. }
  291. /* Write to the shared comm region. */
  292. writew_relaxed(cmd, &generic_comm_base->command);
  293. /* Flip CMD COMPLETE bit */
  294. writew_relaxed(0, &generic_comm_base->status);
  295. pcc_ss_data->platform_owns_pcc = true;
  296. /* Ring doorbell */
  297. ret = mbox_send_message(pcc_ss_data->pcc_channel->mchan, &cmd);
  298. if (ret < 0) {
  299. pr_err("Err sending PCC mbox message. ss: %d cmd:%d, ret:%d\n",
  300. pcc_ss_id, cmd, ret);
  301. goto end;
  302. }
  303. /* wait for completion and check for PCC error bit */
  304. ret = check_pcc_chan(pcc_ss_id, true);
  305. if (pcc_ss_data->pcc_mrtt)
  306. pcc_ss_data->last_cmd_cmpl_time = ktime_get();
  307. if (pcc_ss_data->pcc_channel->mchan->mbox->txdone_irq)
  308. mbox_chan_txdone(pcc_ss_data->pcc_channel->mchan, ret);
  309. else
  310. mbox_client_txdone(pcc_ss_data->pcc_channel->mchan, ret);
  311. end:
  312. if (cmd == CMD_WRITE) {
  313. if (unlikely(ret)) {
  314. for_each_online_cpu(i) {
  315. struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i);
  316. if (!desc)
  317. continue;
  318. if (desc->write_cmd_id == pcc_ss_data->pcc_write_cnt)
  319. desc->write_cmd_status = ret;
  320. }
  321. }
  322. pcc_ss_data->pcc_write_cnt++;
  323. wake_up_all(&pcc_ss_data->pcc_write_wait_q);
  324. }
  325. return ret;
  326. }
  327. static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
  328. {
  329. if (ret < 0)
  330. pr_debug("TX did not complete: CMD sent:%x, ret:%d\n",
  331. *(u16 *)msg, ret);
  332. else
  333. pr_debug("TX completed. CMD sent:%x, ret:%d\n",
  334. *(u16 *)msg, ret);
  335. }
  336. static struct mbox_client cppc_mbox_cl = {
  337. .tx_done = cppc_chan_tx_done,
  338. .knows_txdone = true,
  339. };
  340. static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle)
  341. {
  342. int result = -EFAULT;
  343. acpi_status status = AE_OK;
  344. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  345. struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
  346. struct acpi_buffer state = {0, NULL};
  347. union acpi_object *psd = NULL;
  348. struct acpi_psd_package *pdomain;
  349. status = acpi_evaluate_object_typed(handle, "_PSD", NULL,
  350. &buffer, ACPI_TYPE_PACKAGE);
  351. if (status == AE_NOT_FOUND) /* _PSD is optional */
  352. return 0;
  353. if (ACPI_FAILURE(status))
  354. return -ENODEV;
  355. psd = buffer.pointer;
  356. if (!psd || psd->package.count != 1) {
  357. pr_debug("Invalid _PSD data\n");
  358. goto end;
  359. }
  360. pdomain = &(cpc_ptr->domain_info);
  361. state.length = sizeof(struct acpi_psd_package);
  362. state.pointer = pdomain;
  363. status = acpi_extract_package(&(psd->package.elements[0]),
  364. &format, &state);
  365. if (ACPI_FAILURE(status)) {
  366. pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id);
  367. goto end;
  368. }
  369. if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
  370. pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id);
  371. goto end;
  372. }
  373. if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
  374. pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id);
  375. goto end;
  376. }
  377. if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
  378. pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
  379. pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
  380. pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id);
  381. goto end;
  382. }
  383. result = 0;
  384. end:
  385. kfree(buffer.pointer);
  386. return result;
  387. }
  388. bool acpi_cpc_valid(void)
  389. {
  390. struct cpc_desc *cpc_ptr;
  391. int cpu;
  392. if (acpi_disabled)
  393. return false;
  394. for_each_online_cpu(cpu) {
  395. cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
  396. if (!cpc_ptr)
  397. return false;
  398. }
  399. return true;
  400. }
  401. EXPORT_SYMBOL_GPL(acpi_cpc_valid);
  402. bool cppc_allow_fast_switch(void)
  403. {
  404. struct cpc_register_resource *desired_reg;
  405. struct cpc_desc *cpc_ptr;
  406. int cpu;
  407. for_each_online_cpu(cpu) {
  408. cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
  409. desired_reg = &cpc_ptr->cpc_regs[DESIRED_PERF];
  410. if (!CPC_IN_SYSTEM_MEMORY(desired_reg) &&
  411. !CPC_IN_SYSTEM_IO(desired_reg))
  412. return false;
  413. }
  414. return true;
  415. }
  416. EXPORT_SYMBOL_GPL(cppc_allow_fast_switch);
  417. /**
  418. * acpi_get_psd_map - Map the CPUs in the freq domain of a given cpu
  419. * @cpu: Find all CPUs that share a domain with cpu.
  420. * @cpu_data: Pointer to CPU specific CPPC data including PSD info.
  421. *
  422. * Return: 0 for success or negative value for err.
  423. */
  424. int acpi_get_psd_map(unsigned int cpu, struct cppc_cpudata *cpu_data)
  425. {
  426. struct cpc_desc *cpc_ptr, *match_cpc_ptr;
  427. struct acpi_psd_package *match_pdomain;
  428. struct acpi_psd_package *pdomain;
  429. int count_target, i;
  430. /*
  431. * Now that we have _PSD data from all CPUs, let's setup P-state
  432. * domain info.
  433. */
  434. cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
  435. if (!cpc_ptr)
  436. return -EFAULT;
  437. pdomain = &(cpc_ptr->domain_info);
  438. cpumask_set_cpu(cpu, cpu_data->shared_cpu_map);
  439. if (pdomain->num_processors <= 1)
  440. return 0;
  441. /* Validate the Domain info */
  442. count_target = pdomain->num_processors;
  443. if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
  444. cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  445. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
  446. cpu_data->shared_type = CPUFREQ_SHARED_TYPE_HW;
  447. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
  448. cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  449. for_each_online_cpu(i) {
  450. if (i == cpu)
  451. continue;
  452. match_cpc_ptr = per_cpu(cpc_desc_ptr, i);
  453. if (!match_cpc_ptr)
  454. goto err_fault;
  455. match_pdomain = &(match_cpc_ptr->domain_info);
  456. if (match_pdomain->domain != pdomain->domain)
  457. continue;
  458. /* Here i and cpu are in the same domain */
  459. if (match_pdomain->num_processors != count_target)
  460. goto err_fault;
  461. if (pdomain->coord_type != match_pdomain->coord_type)
  462. goto err_fault;
  463. cpumask_set_cpu(i, cpu_data->shared_cpu_map);
  464. }
  465. return 0;
  466. err_fault:
  467. /* Assume no coordination on any error parsing domain info */
  468. cpumask_clear(cpu_data->shared_cpu_map);
  469. cpumask_set_cpu(cpu, cpu_data->shared_cpu_map);
  470. cpu_data->shared_type = CPUFREQ_SHARED_TYPE_NONE;
  471. return -EFAULT;
  472. }
  473. EXPORT_SYMBOL_GPL(acpi_get_psd_map);
  474. static int register_pcc_channel(int pcc_ss_idx)
  475. {
  476. struct pcc_mbox_chan *pcc_chan;
  477. u64 usecs_lat;
  478. if (pcc_ss_idx >= 0) {
  479. pcc_chan = pcc_mbox_request_channel(&cppc_mbox_cl, pcc_ss_idx);
  480. if (IS_ERR(pcc_chan)) {
  481. pr_err("Failed to find PCC channel for subspace %d\n",
  482. pcc_ss_idx);
  483. return -ENODEV;
  484. }
  485. pcc_data[pcc_ss_idx]->pcc_channel = pcc_chan;
  486. /*
  487. * cppc_ss->latency is just a Nominal value. In reality
  488. * the remote processor could be much slower to reply.
  489. * So add an arbitrary amount of wait on top of Nominal.
  490. */
  491. usecs_lat = NUM_RETRIES * pcc_chan->latency;
  492. pcc_data[pcc_ss_idx]->deadline_us = usecs_lat;
  493. pcc_data[pcc_ss_idx]->pcc_mrtt = pcc_chan->min_turnaround_time;
  494. pcc_data[pcc_ss_idx]->pcc_mpar = pcc_chan->max_access_rate;
  495. pcc_data[pcc_ss_idx]->pcc_nominal = pcc_chan->latency;
  496. /* Set flag so that we don't come here for each CPU. */
  497. pcc_data[pcc_ss_idx]->pcc_channel_acquired = true;
  498. }
  499. return 0;
  500. }
  501. /**
  502. * cpc_ffh_supported() - check if FFH reading supported
  503. *
  504. * Check if the architecture has support for functional fixed hardware
  505. * read/write capability.
  506. *
  507. * Return: true for supported, false for not supported
  508. */
  509. bool __weak cpc_ffh_supported(void)
  510. {
  511. return false;
  512. }
  513. /**
  514. * cpc_supported_by_cpu() - check if CPPC is supported by CPU
  515. *
  516. * Check if the architectural support for CPPC is present even
  517. * if the _OSC hasn't prescribed it
  518. *
  519. * Return: true for supported, false for not supported
  520. */
  521. bool __weak cpc_supported_by_cpu(void)
  522. {
  523. return false;
  524. }
  525. /**
  526. * pcc_data_alloc() - Allocate the pcc_data memory for pcc subspace
  527. * @pcc_ss_id: PCC Subspace index as in the PCC client ACPI package.
  528. *
  529. * Check and allocate the cppc_pcc_data memory.
  530. * In some processor configurations it is possible that same subspace
  531. * is shared between multiple CPUs. This is seen especially in CPUs
  532. * with hardware multi-threading support.
  533. *
  534. * Return: 0 for success, errno for failure
  535. */
  536. static int pcc_data_alloc(int pcc_ss_id)
  537. {
  538. if (pcc_ss_id < 0 || pcc_ss_id >= MAX_PCC_SUBSPACES)
  539. return -EINVAL;
  540. if (pcc_data[pcc_ss_id]) {
  541. pcc_data[pcc_ss_id]->refcount++;
  542. } else {
  543. pcc_data[pcc_ss_id] = kzalloc_obj(struct cppc_pcc_data);
  544. if (!pcc_data[pcc_ss_id])
  545. return -ENOMEM;
  546. pcc_data[pcc_ss_id]->refcount++;
  547. }
  548. return 0;
  549. }
  550. /*
  551. * An example CPC table looks like the following.
  552. *
  553. * Name (_CPC, Package() {
  554. * 17, // NumEntries
  555. * 1, // Revision
  556. * ResourceTemplate() {Register(PCC, 32, 0, 0x120, 2)}, // Highest Performance
  557. * ResourceTemplate() {Register(PCC, 32, 0, 0x124, 2)}, // Nominal Performance
  558. * ResourceTemplate() {Register(PCC, 32, 0, 0x128, 2)}, // Lowest Nonlinear Performance
  559. * ResourceTemplate() {Register(PCC, 32, 0, 0x12C, 2)}, // Lowest Performance
  560. * ResourceTemplate() {Register(PCC, 32, 0, 0x130, 2)}, // Guaranteed Performance Register
  561. * ResourceTemplate() {Register(PCC, 32, 0, 0x110, 2)}, // Desired Performance Register
  562. * ResourceTemplate() {Register(SystemMemory, 0, 0, 0, 0)},
  563. * ...
  564. * ...
  565. * ...
  566. * }
  567. * Each Register() encodes how to access that specific register.
  568. * e.g. a sample PCC entry has the following encoding:
  569. *
  570. * Register (
  571. * PCC, // AddressSpaceKeyword
  572. * 8, // RegisterBitWidth
  573. * 8, // RegisterBitOffset
  574. * 0x30, // RegisterAddress
  575. * 9, // AccessSize (subspace ID)
  576. * )
  577. */
  578. /**
  579. * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
  580. * @pr: Ptr to acpi_processor containing this CPU's logical ID.
  581. *
  582. * Return: 0 for success or negative value for err.
  583. */
  584. int acpi_cppc_processor_probe(struct acpi_processor *pr)
  585. {
  586. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  587. union acpi_object *out_obj, *cpc_obj;
  588. struct cpc_desc *cpc_ptr;
  589. struct cpc_reg *gas_t;
  590. struct device *cpu_dev;
  591. acpi_handle handle = pr->handle;
  592. unsigned int num_ent, i, cpc_rev;
  593. int pcc_subspace_id = -1;
  594. acpi_status status;
  595. int ret = -ENODATA;
  596. if (!osc_sb_cppc2_support_acked) {
  597. pr_debug("CPPC v2 _OSC not acked\n");
  598. if (!cpc_supported_by_cpu()) {
  599. pr_debug("CPPC is not supported by the CPU\n");
  600. return -ENODEV;
  601. }
  602. }
  603. /* Parse the ACPI _CPC table for this CPU. */
  604. status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
  605. ACPI_TYPE_PACKAGE);
  606. if (ACPI_FAILURE(status)) {
  607. ret = -ENODEV;
  608. goto out_buf_free;
  609. }
  610. out_obj = (union acpi_object *) output.pointer;
  611. cpc_ptr = kzalloc_obj(struct cpc_desc);
  612. if (!cpc_ptr) {
  613. ret = -ENOMEM;
  614. goto out_buf_free;
  615. }
  616. /* First entry is NumEntries. */
  617. cpc_obj = &out_obj->package.elements[0];
  618. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  619. num_ent = cpc_obj->integer.value;
  620. if (num_ent <= 1) {
  621. pr_debug("Unexpected _CPC NumEntries value (%d) for CPU:%d\n",
  622. num_ent, pr->id);
  623. goto out_free;
  624. }
  625. } else {
  626. pr_debug("Unexpected _CPC NumEntries entry type (%d) for CPU:%d\n",
  627. cpc_obj->type, pr->id);
  628. goto out_free;
  629. }
  630. /* Second entry should be revision. */
  631. cpc_obj = &out_obj->package.elements[1];
  632. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  633. cpc_rev = cpc_obj->integer.value;
  634. } else {
  635. pr_debug("Unexpected _CPC Revision entry type (%d) for CPU:%d\n",
  636. cpc_obj->type, pr->id);
  637. goto out_free;
  638. }
  639. if (cpc_rev < CPPC_V2_REV) {
  640. pr_debug("Unsupported _CPC Revision (%d) for CPU:%d\n", cpc_rev,
  641. pr->id);
  642. goto out_free;
  643. }
  644. /*
  645. * Disregard _CPC if the number of entries in the return package is not
  646. * as expected, but support future revisions being proper supersets of
  647. * the v3 and only causing more entries to be returned by _CPC.
  648. */
  649. if ((cpc_rev == CPPC_V2_REV && num_ent != CPPC_V2_NUM_ENT) ||
  650. (cpc_rev == CPPC_V3_REV && num_ent != CPPC_V3_NUM_ENT) ||
  651. (cpc_rev > CPPC_V3_REV && num_ent <= CPPC_V3_NUM_ENT)) {
  652. pr_debug("Unexpected number of _CPC return package entries (%d) for CPU:%d\n",
  653. num_ent, pr->id);
  654. goto out_free;
  655. }
  656. if (cpc_rev > CPPC_V3_REV) {
  657. num_ent = CPPC_V3_NUM_ENT;
  658. cpc_rev = CPPC_V3_REV;
  659. }
  660. cpc_ptr->num_entries = num_ent;
  661. cpc_ptr->version = cpc_rev;
  662. /* Iterate through remaining entries in _CPC */
  663. for (i = 2; i < num_ent; i++) {
  664. cpc_obj = &out_obj->package.elements[i];
  665. if (cpc_obj->type == ACPI_TYPE_INTEGER) {
  666. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
  667. cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value;
  668. } else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
  669. gas_t = (struct cpc_reg *)
  670. cpc_obj->buffer.pointer;
  671. /*
  672. * The PCC Subspace index is encoded inside
  673. * the CPC table entries. The same PCC index
  674. * will be used for all the PCC entries,
  675. * so extract it only once.
  676. */
  677. if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  678. if (pcc_subspace_id < 0) {
  679. pcc_subspace_id = gas_t->access_width;
  680. if (pcc_data_alloc(pcc_subspace_id))
  681. goto out_free;
  682. } else if (pcc_subspace_id != gas_t->access_width) {
  683. pr_debug("Mismatched PCC ids in _CPC for CPU:%d\n",
  684. pr->id);
  685. goto out_free;
  686. }
  687. } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  688. if (gas_t->address) {
  689. void __iomem *addr;
  690. size_t access_width;
  691. if (!osc_cpc_flexible_adr_space_confirmed) {
  692. pr_debug("Flexible address space capability not supported\n");
  693. if (!cpc_supported_by_cpu())
  694. goto out_free;
  695. }
  696. access_width = GET_BIT_WIDTH(gas_t) / 8;
  697. addr = ioremap(gas_t->address, access_width);
  698. if (!addr)
  699. goto out_free;
  700. cpc_ptr->cpc_regs[i-2].sys_mem_vaddr = addr;
  701. }
  702. } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  703. if (gas_t->access_width < 1 || gas_t->access_width > 3) {
  704. /*
  705. * 1 = 8-bit, 2 = 16-bit, and 3 = 32-bit.
  706. * SystemIO doesn't implement 64-bit
  707. * registers.
  708. */
  709. pr_debug("Invalid access width %d for SystemIO register in _CPC\n",
  710. gas_t->access_width);
  711. goto out_free;
  712. }
  713. if (gas_t->address & OVER_16BTS_MASK) {
  714. /* SystemIO registers use 16-bit integer addresses */
  715. pr_debug("Invalid IO port %llu for SystemIO register in _CPC\n",
  716. gas_t->address);
  717. goto out_free;
  718. }
  719. if (!osc_cpc_flexible_adr_space_confirmed) {
  720. pr_debug("Flexible address space capability not supported\n");
  721. if (!cpc_supported_by_cpu())
  722. goto out_free;
  723. }
  724. } else {
  725. if (gas_t->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported()) {
  726. /* Support only PCC, SystemMemory, SystemIO, and FFH type regs. */
  727. pr_debug("Unsupported register type (%d) in _CPC\n",
  728. gas_t->space_id);
  729. goto out_free;
  730. }
  731. }
  732. cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER;
  733. memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t));
  734. } else {
  735. pr_debug("Invalid entry type (%d) in _CPC for CPU:%d\n",
  736. i, pr->id);
  737. goto out_free;
  738. }
  739. }
  740. per_cpu(cpu_pcc_subspace_idx, pr->id) = pcc_subspace_id;
  741. /*
  742. * Initialize the remaining cpc_regs as unsupported.
  743. * Example: In case FW exposes CPPC v2, the below loop will initialize
  744. * LOWEST_FREQ and NOMINAL_FREQ regs as unsupported
  745. */
  746. for (i = num_ent - 2; i < MAX_CPC_REG_ENT; i++) {
  747. cpc_ptr->cpc_regs[i].type = ACPI_TYPE_INTEGER;
  748. cpc_ptr->cpc_regs[i].cpc_entry.int_value = 0;
  749. }
  750. /* Store CPU Logical ID */
  751. cpc_ptr->cpu_id = pr->id;
  752. raw_spin_lock_init(&cpc_ptr->rmw_lock);
  753. /* Parse PSD data for this CPU */
  754. ret = acpi_get_psd(cpc_ptr, handle);
  755. if (ret)
  756. goto out_free;
  757. /* Register PCC channel once for all PCC subspace ID. */
  758. if (pcc_subspace_id >= 0 && !pcc_data[pcc_subspace_id]->pcc_channel_acquired) {
  759. ret = register_pcc_channel(pcc_subspace_id);
  760. if (ret)
  761. goto out_free;
  762. init_rwsem(&pcc_data[pcc_subspace_id]->pcc_lock);
  763. init_waitqueue_head(&pcc_data[pcc_subspace_id]->pcc_write_wait_q);
  764. }
  765. /* Everything looks okay */
  766. pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
  767. /* Add per logical CPU nodes for reading its feedback counters. */
  768. cpu_dev = get_cpu_device(pr->id);
  769. if (!cpu_dev) {
  770. ret = -EINVAL;
  771. goto out_free;
  772. }
  773. /* Plug PSD data into this CPU's CPC descriptor. */
  774. per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr;
  775. ret = kobject_init_and_add(&cpc_ptr->kobj, &cppc_ktype, &cpu_dev->kobj,
  776. "acpi_cppc");
  777. if (ret) {
  778. per_cpu(cpc_desc_ptr, pr->id) = NULL;
  779. kobject_put(&cpc_ptr->kobj);
  780. goto out_free;
  781. }
  782. kfree(output.pointer);
  783. return 0;
  784. out_free:
  785. /* Free all the mapped sys mem areas for this CPU */
  786. for (i = 2; i < cpc_ptr->num_entries; i++) {
  787. void __iomem *addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
  788. if (addr)
  789. iounmap(addr);
  790. }
  791. kfree(cpc_ptr);
  792. out_buf_free:
  793. kfree(output.pointer);
  794. return ret;
  795. }
  796. EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
  797. /**
  798. * acpi_cppc_processor_exit - Cleanup CPC structs.
  799. * @pr: Ptr to acpi_processor containing this CPU's logical ID.
  800. *
  801. * Return: Void
  802. */
  803. void acpi_cppc_processor_exit(struct acpi_processor *pr)
  804. {
  805. struct cpc_desc *cpc_ptr;
  806. unsigned int i;
  807. void __iomem *addr;
  808. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, pr->id);
  809. if (pcc_ss_id >= 0 && pcc_data[pcc_ss_id]) {
  810. if (pcc_data[pcc_ss_id]->pcc_channel_acquired) {
  811. pcc_data[pcc_ss_id]->refcount--;
  812. if (!pcc_data[pcc_ss_id]->refcount) {
  813. pcc_mbox_free_channel(pcc_data[pcc_ss_id]->pcc_channel);
  814. kfree(pcc_data[pcc_ss_id]);
  815. pcc_data[pcc_ss_id] = NULL;
  816. }
  817. }
  818. }
  819. cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
  820. if (!cpc_ptr)
  821. return;
  822. /* Free all the mapped sys mem areas for this CPU */
  823. for (i = 2; i < cpc_ptr->num_entries; i++) {
  824. addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
  825. if (addr)
  826. iounmap(addr);
  827. }
  828. kobject_put(&cpc_ptr->kobj);
  829. kfree(cpc_ptr);
  830. }
  831. EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
  832. /**
  833. * cpc_read_ffh() - Read FFH register
  834. * @cpunum: CPU number to read
  835. * @reg: cppc register information
  836. * @val: place holder for return value
  837. *
  838. * Read bit_width bits from a specified address and bit_offset
  839. *
  840. * Return: 0 for success and error code
  841. */
  842. int __weak cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
  843. {
  844. return -ENOTSUPP;
  845. }
  846. /**
  847. * cpc_write_ffh() - Write FFH register
  848. * @cpunum: CPU number to write
  849. * @reg: cppc register information
  850. * @val: value to write
  851. *
  852. * Write value of bit_width bits to a specified address and bit_offset
  853. *
  854. * Return: 0 for success and error code
  855. */
  856. int __weak cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
  857. {
  858. return -ENOTSUPP;
  859. }
  860. /*
  861. * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
  862. * as fast as possible. We have already mapped the PCC subspace during init, so
  863. * we can directly write to it.
  864. */
  865. static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val)
  866. {
  867. void __iomem *vaddr = NULL;
  868. int size;
  869. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  870. struct cpc_reg *reg = &reg_res->cpc_entry.reg;
  871. if (reg_res->type == ACPI_TYPE_INTEGER) {
  872. *val = reg_res->cpc_entry.int_value;
  873. return 0;
  874. }
  875. *val = 0;
  876. size = GET_BIT_WIDTH(reg);
  877. if (IS_ENABLED(CONFIG_HAS_IOPORT) &&
  878. reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  879. u32 val_u32;
  880. acpi_status status;
  881. status = acpi_os_read_port((acpi_io_address)reg->address,
  882. &val_u32, size);
  883. if (ACPI_FAILURE(status)) {
  884. pr_debug("Error: Failed to read SystemIO port %llx\n",
  885. reg->address);
  886. return -EFAULT;
  887. }
  888. *val = val_u32;
  889. return 0;
  890. } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0) {
  891. /*
  892. * For registers in PCC space, the register size is determined
  893. * by the bit width field; the access size is used to indicate
  894. * the PCC subspace id.
  895. */
  896. size = reg->bit_width;
  897. vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
  898. }
  899. else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  900. vaddr = reg_res->sys_mem_vaddr;
  901. else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
  902. return cpc_read_ffh(cpu, reg, val);
  903. else
  904. return acpi_os_read_memory((acpi_physical_address)reg->address,
  905. val, size);
  906. switch (size) {
  907. case 8:
  908. *val = readb_relaxed(vaddr);
  909. break;
  910. case 16:
  911. *val = readw_relaxed(vaddr);
  912. break;
  913. case 32:
  914. *val = readl_relaxed(vaddr);
  915. break;
  916. case 64:
  917. *val = readq_relaxed(vaddr);
  918. break;
  919. default:
  920. if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  921. pr_debug("Error: Cannot read %u bit width from system memory: 0x%llx\n",
  922. size, reg->address);
  923. } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  924. pr_debug("Error: Cannot read %u bit width from PCC for ss: %d\n",
  925. size, pcc_ss_id);
  926. }
  927. return -EFAULT;
  928. }
  929. if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  930. *val = MASK_VAL_READ(reg, *val);
  931. return 0;
  932. }
  933. static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)
  934. {
  935. int ret_val = 0;
  936. int size;
  937. u64 prev_val;
  938. void __iomem *vaddr = NULL;
  939. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  940. struct cpc_reg *reg = &reg_res->cpc_entry.reg;
  941. struct cpc_desc *cpc_desc;
  942. unsigned long flags;
  943. size = GET_BIT_WIDTH(reg);
  944. if (IS_ENABLED(CONFIG_HAS_IOPORT) &&
  945. reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  946. acpi_status status;
  947. status = acpi_os_write_port((acpi_io_address)reg->address,
  948. (u32)val, size);
  949. if (ACPI_FAILURE(status)) {
  950. pr_debug("Error: Failed to write SystemIO port %llx\n",
  951. reg->address);
  952. return -EFAULT;
  953. }
  954. return 0;
  955. } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0) {
  956. /*
  957. * For registers in PCC space, the register size is determined
  958. * by the bit width field; the access size is used to indicate
  959. * the PCC subspace id.
  960. */
  961. size = reg->bit_width;
  962. vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
  963. }
  964. else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  965. vaddr = reg_res->sys_mem_vaddr;
  966. else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
  967. return cpc_write_ffh(cpu, reg, val);
  968. else
  969. return acpi_os_write_memory((acpi_physical_address)reg->address,
  970. val, size);
  971. if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  972. cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  973. if (!cpc_desc) {
  974. pr_debug("No CPC descriptor for CPU:%d\n", cpu);
  975. return -ENODEV;
  976. }
  977. raw_spin_lock_irqsave(&cpc_desc->rmw_lock, flags);
  978. switch (size) {
  979. case 8:
  980. prev_val = readb_relaxed(vaddr);
  981. break;
  982. case 16:
  983. prev_val = readw_relaxed(vaddr);
  984. break;
  985. case 32:
  986. prev_val = readl_relaxed(vaddr);
  987. break;
  988. case 64:
  989. prev_val = readq_relaxed(vaddr);
  990. break;
  991. default:
  992. raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, flags);
  993. return -EFAULT;
  994. }
  995. val = MASK_VAL_WRITE(reg, prev_val, val);
  996. }
  997. switch (size) {
  998. case 8:
  999. writeb_relaxed(val, vaddr);
  1000. break;
  1001. case 16:
  1002. writew_relaxed(val, vaddr);
  1003. break;
  1004. case 32:
  1005. writel_relaxed(val, vaddr);
  1006. break;
  1007. case 64:
  1008. writeq_relaxed(val, vaddr);
  1009. break;
  1010. default:
  1011. if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  1012. pr_debug("Error: Cannot write %u bit width to system memory: 0x%llx\n",
  1013. size, reg->address);
  1014. } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
  1015. pr_debug("Error: Cannot write %u bit width to PCC for ss: %d\n",
  1016. size, pcc_ss_id);
  1017. }
  1018. ret_val = -EFAULT;
  1019. break;
  1020. }
  1021. if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  1022. raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, flags);
  1023. return ret_val;
  1024. }
  1025. static int cppc_get_reg_val_in_pcc(int cpu, struct cpc_register_resource *reg, u64 *val)
  1026. {
  1027. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  1028. struct cppc_pcc_data *pcc_ss_data = NULL;
  1029. int ret;
  1030. if (pcc_ss_id < 0) {
  1031. pr_debug("Invalid pcc_ss_id\n");
  1032. return -ENODEV;
  1033. }
  1034. pcc_ss_data = pcc_data[pcc_ss_id];
  1035. down_write(&pcc_ss_data->pcc_lock);
  1036. if (send_pcc_cmd(pcc_ss_id, CMD_READ) >= 0)
  1037. ret = cpc_read(cpu, reg, val);
  1038. else
  1039. ret = -EIO;
  1040. up_write(&pcc_ss_data->pcc_lock);
  1041. return ret;
  1042. }
  1043. static int cppc_get_reg_val(int cpu, enum cppc_regs reg_idx, u64 *val)
  1044. {
  1045. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  1046. struct cpc_register_resource *reg;
  1047. if (val == NULL)
  1048. return -EINVAL;
  1049. if (!cpc_desc) {
  1050. pr_debug("No CPC descriptor for CPU:%d\n", cpu);
  1051. return -ENODEV;
  1052. }
  1053. reg = &cpc_desc->cpc_regs[reg_idx];
  1054. if ((reg->type == ACPI_TYPE_INTEGER && IS_OPTIONAL_CPC_REG(reg_idx) &&
  1055. !reg->cpc_entry.int_value) || (reg->type != ACPI_TYPE_INTEGER &&
  1056. IS_NULL_REG(&reg->cpc_entry.reg))) {
  1057. pr_debug("CPC register is not supported\n");
  1058. return -EOPNOTSUPP;
  1059. }
  1060. if (CPC_IN_PCC(reg))
  1061. return cppc_get_reg_val_in_pcc(cpu, reg, val);
  1062. return cpc_read(cpu, reg, val);
  1063. }
  1064. static int cppc_set_reg_val_in_pcc(int cpu, struct cpc_register_resource *reg, u64 val)
  1065. {
  1066. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  1067. struct cppc_pcc_data *pcc_ss_data = NULL;
  1068. int ret;
  1069. if (pcc_ss_id < 0) {
  1070. pr_debug("Invalid pcc_ss_id\n");
  1071. return -ENODEV;
  1072. }
  1073. ret = cpc_write(cpu, reg, val);
  1074. if (ret)
  1075. return ret;
  1076. pcc_ss_data = pcc_data[pcc_ss_id];
  1077. down_write(&pcc_ss_data->pcc_lock);
  1078. /* after writing CPC, transfer the ownership of PCC to platform */
  1079. ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
  1080. up_write(&pcc_ss_data->pcc_lock);
  1081. return ret;
  1082. }
  1083. static int cppc_set_reg_val(int cpu, enum cppc_regs reg_idx, u64 val)
  1084. {
  1085. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  1086. struct cpc_register_resource *reg;
  1087. if (!cpc_desc) {
  1088. pr_debug("No CPC descriptor for CPU:%d\n", cpu);
  1089. return -ENODEV;
  1090. }
  1091. reg = &cpc_desc->cpc_regs[reg_idx];
  1092. /* if a register is writeable, it must be a buffer and not null */
  1093. if ((reg->type != ACPI_TYPE_BUFFER) || IS_NULL_REG(&reg->cpc_entry.reg)) {
  1094. pr_debug("CPC register is not supported\n");
  1095. return -EOPNOTSUPP;
  1096. }
  1097. if (CPC_IN_PCC(reg))
  1098. return cppc_set_reg_val_in_pcc(cpu, reg, val);
  1099. return cpc_write(cpu, reg, val);
  1100. }
  1101. /**
  1102. * cppc_get_desired_perf - Get the desired performance register value.
  1103. * @cpunum: CPU from which to get desired performance.
  1104. * @desired_perf: Return address.
  1105. *
  1106. * Return: 0 for success, -EIO otherwise.
  1107. */
  1108. int cppc_get_desired_perf(int cpunum, u64 *desired_perf)
  1109. {
  1110. return cppc_get_reg_val(cpunum, DESIRED_PERF, desired_perf);
  1111. }
  1112. EXPORT_SYMBOL_GPL(cppc_get_desired_perf);
  1113. /**
  1114. * cppc_get_nominal_perf - Get the nominal performance register value.
  1115. * @cpunum: CPU from which to get nominal performance.
  1116. * @nominal_perf: Return address.
  1117. *
  1118. * Return: 0 for success, -EIO otherwise.
  1119. */
  1120. int cppc_get_nominal_perf(int cpunum, u64 *nominal_perf)
  1121. {
  1122. return cppc_get_reg_val(cpunum, NOMINAL_PERF, nominal_perf);
  1123. }
  1124. /**
  1125. * cppc_get_highest_perf - Get the highest performance register value.
  1126. * @cpunum: CPU from which to get highest performance.
  1127. * @highest_perf: Return address.
  1128. *
  1129. * Return: 0 for success, -EIO otherwise.
  1130. */
  1131. int cppc_get_highest_perf(int cpunum, u64 *highest_perf)
  1132. {
  1133. return cppc_get_reg_val(cpunum, HIGHEST_PERF, highest_perf);
  1134. }
  1135. EXPORT_SYMBOL_GPL(cppc_get_highest_perf);
  1136. /**
  1137. * cppc_get_epp_perf - Get the epp register value.
  1138. * @cpunum: CPU from which to get epp preference value.
  1139. * @epp_perf: Return address.
  1140. *
  1141. * Return: 0 for success, -EIO otherwise.
  1142. */
  1143. int cppc_get_epp_perf(int cpunum, u64 *epp_perf)
  1144. {
  1145. return cppc_get_reg_val(cpunum, ENERGY_PERF, epp_perf);
  1146. }
  1147. EXPORT_SYMBOL_GPL(cppc_get_epp_perf);
  1148. /**
  1149. * cppc_get_perf_caps - Get a CPU's performance capabilities.
  1150. * @cpunum: CPU from which to get capabilities info.
  1151. * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
  1152. *
  1153. * Return: 0 for success with perf_caps populated else -ERRNO.
  1154. */
  1155. int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
  1156. {
  1157. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  1158. struct cpc_register_resource *highest_reg, *lowest_reg,
  1159. *lowest_non_linear_reg, *nominal_reg, *guaranteed_reg,
  1160. *low_freq_reg = NULL, *nom_freq_reg = NULL;
  1161. u64 high, low, guaranteed, nom, min_nonlinear, low_f = 0, nom_f = 0;
  1162. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
  1163. struct cppc_pcc_data *pcc_ss_data = NULL;
  1164. int ret = 0, regs_in_pcc = 0;
  1165. if (!cpc_desc) {
  1166. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  1167. return -ENODEV;
  1168. }
  1169. highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
  1170. lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
  1171. lowest_non_linear_reg = &cpc_desc->cpc_regs[LOW_NON_LINEAR_PERF];
  1172. nominal_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
  1173. low_freq_reg = &cpc_desc->cpc_regs[LOWEST_FREQ];
  1174. nom_freq_reg = &cpc_desc->cpc_regs[NOMINAL_FREQ];
  1175. guaranteed_reg = &cpc_desc->cpc_regs[GUARANTEED_PERF];
  1176. /* Are any of the regs PCC ?*/
  1177. if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) ||
  1178. CPC_IN_PCC(lowest_non_linear_reg) || CPC_IN_PCC(nominal_reg) ||
  1179. CPC_IN_PCC(low_freq_reg) || CPC_IN_PCC(nom_freq_reg) ||
  1180. CPC_IN_PCC(guaranteed_reg)) {
  1181. if (pcc_ss_id < 0) {
  1182. pr_debug("Invalid pcc_ss_id\n");
  1183. return -ENODEV;
  1184. }
  1185. pcc_ss_data = pcc_data[pcc_ss_id];
  1186. regs_in_pcc = 1;
  1187. down_write(&pcc_ss_data->pcc_lock);
  1188. /* Ring doorbell once to update PCC subspace */
  1189. if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
  1190. ret = -EIO;
  1191. goto out_err;
  1192. }
  1193. }
  1194. cpc_read(cpunum, highest_reg, &high);
  1195. perf_caps->highest_perf = high;
  1196. cpc_read(cpunum, lowest_reg, &low);
  1197. perf_caps->lowest_perf = low;
  1198. cpc_read(cpunum, nominal_reg, &nom);
  1199. perf_caps->nominal_perf = nom;
  1200. if (guaranteed_reg->type != ACPI_TYPE_BUFFER ||
  1201. IS_NULL_REG(&guaranteed_reg->cpc_entry.reg)) {
  1202. perf_caps->guaranteed_perf = 0;
  1203. } else {
  1204. cpc_read(cpunum, guaranteed_reg, &guaranteed);
  1205. perf_caps->guaranteed_perf = guaranteed;
  1206. }
  1207. cpc_read(cpunum, lowest_non_linear_reg, &min_nonlinear);
  1208. perf_caps->lowest_nonlinear_perf = min_nonlinear;
  1209. if (!high || !low || !nom || !min_nonlinear)
  1210. ret = -EFAULT;
  1211. /* Read optional lowest and nominal frequencies if present */
  1212. if (CPC_SUPPORTED(low_freq_reg))
  1213. cpc_read(cpunum, low_freq_reg, &low_f);
  1214. if (CPC_SUPPORTED(nom_freq_reg))
  1215. cpc_read(cpunum, nom_freq_reg, &nom_f);
  1216. perf_caps->lowest_freq = low_f;
  1217. perf_caps->nominal_freq = nom_f;
  1218. out_err:
  1219. if (regs_in_pcc)
  1220. up_write(&pcc_ss_data->pcc_lock);
  1221. return ret;
  1222. }
  1223. EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
  1224. /**
  1225. * cppc_perf_ctrs_in_pcc_cpu - Check if any perf counters of a CPU are in PCC.
  1226. * @cpu: CPU on which to check perf counters.
  1227. *
  1228. * Return: true if any of the counters are in PCC regions, false otherwise
  1229. */
  1230. bool cppc_perf_ctrs_in_pcc_cpu(unsigned int cpu)
  1231. {
  1232. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  1233. struct cpc_register_resource *ref_perf_reg;
  1234. /*
  1235. * If reference perf register is not supported then we should use the
  1236. * nominal perf value
  1237. */
  1238. ref_perf_reg = &cpc_desc->cpc_regs[REFERENCE_PERF];
  1239. if (!CPC_SUPPORTED(ref_perf_reg))
  1240. ref_perf_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
  1241. return CPC_IN_PCC(&cpc_desc->cpc_regs[DELIVERED_CTR]) ||
  1242. CPC_IN_PCC(&cpc_desc->cpc_regs[REFERENCE_CTR]) ||
  1243. CPC_IN_PCC(&cpc_desc->cpc_regs[CTR_WRAP_TIME]) ||
  1244. CPC_IN_PCC(ref_perf_reg);
  1245. }
  1246. EXPORT_SYMBOL_GPL(cppc_perf_ctrs_in_pcc_cpu);
  1247. /**
  1248. * cppc_perf_ctrs_in_pcc - Check if any perf counters are in a PCC region.
  1249. *
  1250. * CPPC has flexibility about how CPU performance counters are accessed.
  1251. * One of the choices is PCC regions, which can have a high access latency. This
  1252. * routine allows callers of cppc_get_perf_ctrs() to know this ahead of time.
  1253. *
  1254. * Return: true if any of the counters are in PCC regions, false otherwise
  1255. */
  1256. bool cppc_perf_ctrs_in_pcc(void)
  1257. {
  1258. int cpu;
  1259. for_each_online_cpu(cpu) {
  1260. if (cppc_perf_ctrs_in_pcc_cpu(cpu))
  1261. return true;
  1262. }
  1263. return false;
  1264. }
  1265. EXPORT_SYMBOL_GPL(cppc_perf_ctrs_in_pcc);
  1266. /**
  1267. * cppc_get_perf_ctrs - Read a CPU's performance feedback counters.
  1268. * @cpunum: CPU from which to read counters.
  1269. * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
  1270. *
  1271. * Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
  1272. */
  1273. int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
  1274. {
  1275. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
  1276. struct cpc_register_resource *delivered_reg, *reference_reg,
  1277. *ref_perf_reg, *ctr_wrap_reg;
  1278. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
  1279. struct cppc_pcc_data *pcc_ss_data = NULL;
  1280. u64 delivered, reference, ref_perf, ctr_wrap_time;
  1281. int ret = 0, regs_in_pcc = 0;
  1282. if (!cpc_desc) {
  1283. pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
  1284. return -ENODEV;
  1285. }
  1286. delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
  1287. reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
  1288. ref_perf_reg = &cpc_desc->cpc_regs[REFERENCE_PERF];
  1289. ctr_wrap_reg = &cpc_desc->cpc_regs[CTR_WRAP_TIME];
  1290. /*
  1291. * If reference perf register is not supported then we should
  1292. * use the nominal perf value
  1293. */
  1294. if (!CPC_SUPPORTED(ref_perf_reg))
  1295. ref_perf_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
  1296. /* Are any of the regs PCC ?*/
  1297. if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) ||
  1298. CPC_IN_PCC(ctr_wrap_reg) || CPC_IN_PCC(ref_perf_reg)) {
  1299. if (pcc_ss_id < 0) {
  1300. pr_debug("Invalid pcc_ss_id\n");
  1301. return -ENODEV;
  1302. }
  1303. pcc_ss_data = pcc_data[pcc_ss_id];
  1304. down_write(&pcc_ss_data->pcc_lock);
  1305. regs_in_pcc = 1;
  1306. /* Ring doorbell once to update PCC subspace */
  1307. if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
  1308. ret = -EIO;
  1309. goto out_err;
  1310. }
  1311. }
  1312. cpc_read(cpunum, delivered_reg, &delivered);
  1313. cpc_read(cpunum, reference_reg, &reference);
  1314. cpc_read(cpunum, ref_perf_reg, &ref_perf);
  1315. /*
  1316. * Per spec, if ctr_wrap_time optional register is unsupported, then the
  1317. * performance counters are assumed to never wrap during the lifetime of
  1318. * platform
  1319. */
  1320. ctr_wrap_time = (u64)(~((u64)0));
  1321. if (CPC_SUPPORTED(ctr_wrap_reg))
  1322. cpc_read(cpunum, ctr_wrap_reg, &ctr_wrap_time);
  1323. if (!delivered || !reference || !ref_perf) {
  1324. ret = -EFAULT;
  1325. goto out_err;
  1326. }
  1327. perf_fb_ctrs->delivered = delivered;
  1328. perf_fb_ctrs->reference = reference;
  1329. perf_fb_ctrs->reference_perf = ref_perf;
  1330. perf_fb_ctrs->wraparound_time = ctr_wrap_time;
  1331. out_err:
  1332. if (regs_in_pcc)
  1333. up_write(&pcc_ss_data->pcc_lock);
  1334. return ret;
  1335. }
  1336. EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
  1337. /*
  1338. * Set Energy Performance Preference Register value through
  1339. * Performance Controls Interface
  1340. */
  1341. int cppc_set_epp_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls, bool enable)
  1342. {
  1343. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  1344. struct cpc_register_resource *epp_set_reg;
  1345. struct cpc_register_resource *auto_sel_reg;
  1346. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  1347. struct cppc_pcc_data *pcc_ss_data = NULL;
  1348. int ret;
  1349. if (!cpc_desc) {
  1350. pr_debug("No CPC descriptor for CPU:%d\n", cpu);
  1351. return -ENODEV;
  1352. }
  1353. auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE];
  1354. epp_set_reg = &cpc_desc->cpc_regs[ENERGY_PERF];
  1355. if (CPC_IN_PCC(epp_set_reg) || CPC_IN_PCC(auto_sel_reg)) {
  1356. if (pcc_ss_id < 0) {
  1357. pr_debug("Invalid pcc_ss_id for CPU:%d\n", cpu);
  1358. return -ENODEV;
  1359. }
  1360. if (CPC_SUPPORTED(auto_sel_reg)) {
  1361. ret = cpc_write(cpu, auto_sel_reg, enable);
  1362. if (ret)
  1363. return ret;
  1364. }
  1365. if (CPC_SUPPORTED(epp_set_reg)) {
  1366. ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf);
  1367. if (ret)
  1368. return ret;
  1369. }
  1370. pcc_ss_data = pcc_data[pcc_ss_id];
  1371. down_write(&pcc_ss_data->pcc_lock);
  1372. /* after writing CPC, transfer the ownership of PCC to platform */
  1373. ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
  1374. up_write(&pcc_ss_data->pcc_lock);
  1375. } else if (osc_cpc_flexible_adr_space_confirmed &&
  1376. CPC_SUPPORTED(epp_set_reg) && CPC_IN_FFH(epp_set_reg)) {
  1377. ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf);
  1378. } else {
  1379. ret = -ENOTSUPP;
  1380. pr_debug("_CPC in PCC and _CPC in FFH are not supported\n");
  1381. }
  1382. return ret;
  1383. }
  1384. EXPORT_SYMBOL_GPL(cppc_set_epp_perf);
  1385. /**
  1386. * cppc_set_epp() - Write the EPP register.
  1387. * @cpu: CPU on which to write register.
  1388. * @epp_val: Value to write to the EPP register.
  1389. */
  1390. int cppc_set_epp(int cpu, u64 epp_val)
  1391. {
  1392. if (epp_val > CPPC_EPP_ENERGY_EFFICIENCY_PREF)
  1393. return -EINVAL;
  1394. return cppc_set_reg_val(cpu, ENERGY_PERF, epp_val);
  1395. }
  1396. EXPORT_SYMBOL_GPL(cppc_set_epp);
  1397. /**
  1398. * cppc_get_auto_act_window() - Read autonomous activity window register.
  1399. * @cpu: CPU from which to read register.
  1400. * @auto_act_window: Return address.
  1401. *
  1402. * According to ACPI 6.5, s8.4.6.1.6, the value read from the autonomous
  1403. * activity window register consists of two parts: a 7 bits value indicate
  1404. * significand and a 3 bits value indicate exponent.
  1405. */
  1406. int cppc_get_auto_act_window(int cpu, u64 *auto_act_window)
  1407. {
  1408. unsigned int exp;
  1409. u64 val, sig;
  1410. int ret;
  1411. if (auto_act_window == NULL)
  1412. return -EINVAL;
  1413. ret = cppc_get_reg_val(cpu, AUTO_ACT_WINDOW, &val);
  1414. if (ret)
  1415. return ret;
  1416. sig = val & CPPC_AUTO_ACT_WINDOW_MAX_SIG;
  1417. exp = (val >> CPPC_AUTO_ACT_WINDOW_SIG_BIT_SIZE) & CPPC_AUTO_ACT_WINDOW_MAX_EXP;
  1418. *auto_act_window = sig * int_pow(10, exp);
  1419. return 0;
  1420. }
  1421. EXPORT_SYMBOL_GPL(cppc_get_auto_act_window);
  1422. /**
  1423. * cppc_set_auto_act_window() - Write autonomous activity window register.
  1424. * @cpu: CPU on which to write register.
  1425. * @auto_act_window: usec value to write to the autonomous activity window register.
  1426. *
  1427. * According to ACPI 6.5, s8.4.6.1.6, the value to write to the autonomous
  1428. * activity window register consists of two parts: a 7 bits value indicate
  1429. * significand and a 3 bits value indicate exponent.
  1430. */
  1431. int cppc_set_auto_act_window(int cpu, u64 auto_act_window)
  1432. {
  1433. /* The max value to store is 1270000000 */
  1434. u64 max_val = CPPC_AUTO_ACT_WINDOW_MAX_SIG * int_pow(10, CPPC_AUTO_ACT_WINDOW_MAX_EXP);
  1435. int exp = 0;
  1436. u64 val;
  1437. if (auto_act_window > max_val)
  1438. return -EINVAL;
  1439. /*
  1440. * The max significand is 127, when auto_act_window is larger than
  1441. * 129, discard the precision of the last digit and increase the
  1442. * exponent by 1.
  1443. */
  1444. while (auto_act_window > CPPC_AUTO_ACT_WINDOW_SIG_CARRY_THRESH) {
  1445. auto_act_window /= 10;
  1446. exp += 1;
  1447. }
  1448. /* For 128 and 129, cut it to 127. */
  1449. if (auto_act_window > CPPC_AUTO_ACT_WINDOW_MAX_SIG)
  1450. auto_act_window = CPPC_AUTO_ACT_WINDOW_MAX_SIG;
  1451. val = (exp << CPPC_AUTO_ACT_WINDOW_SIG_BIT_SIZE) + auto_act_window;
  1452. return cppc_set_reg_val(cpu, AUTO_ACT_WINDOW, val);
  1453. }
  1454. EXPORT_SYMBOL_GPL(cppc_set_auto_act_window);
  1455. /**
  1456. * cppc_get_auto_sel() - Read autonomous selection register.
  1457. * @cpu: CPU from which to read register.
  1458. * @enable: Return address.
  1459. */
  1460. int cppc_get_auto_sel(int cpu, bool *enable)
  1461. {
  1462. u64 auto_sel;
  1463. int ret;
  1464. if (enable == NULL)
  1465. return -EINVAL;
  1466. ret = cppc_get_reg_val(cpu, AUTO_SEL_ENABLE, &auto_sel);
  1467. if (ret)
  1468. return ret;
  1469. *enable = (bool)auto_sel;
  1470. return 0;
  1471. }
  1472. EXPORT_SYMBOL_GPL(cppc_get_auto_sel);
  1473. /**
  1474. * cppc_set_auto_sel - Write autonomous selection register.
  1475. * @cpu : CPU to which to write register.
  1476. * @enable : the desired value of autonomous selection resiter to be updated.
  1477. */
  1478. int cppc_set_auto_sel(int cpu, bool enable)
  1479. {
  1480. return cppc_set_reg_val(cpu, AUTO_SEL_ENABLE, enable);
  1481. }
  1482. EXPORT_SYMBOL_GPL(cppc_set_auto_sel);
  1483. /**
  1484. * cppc_set_enable - Set to enable CPPC on the processor by writing the
  1485. * Continuous Performance Control package EnableRegister field.
  1486. * @cpu: CPU for which to enable CPPC register.
  1487. * @enable: 0 - disable, 1 - enable CPPC feature on the processor.
  1488. *
  1489. * Return: 0 for success, -ERRNO or -EIO otherwise.
  1490. */
  1491. int cppc_set_enable(int cpu, bool enable)
  1492. {
  1493. return cppc_set_reg_val(cpu, ENABLE, enable);
  1494. }
  1495. EXPORT_SYMBOL_GPL(cppc_set_enable);
  1496. /**
  1497. * cppc_set_perf - Set a CPU's performance controls.
  1498. * @cpu: CPU for which to set performance controls.
  1499. * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
  1500. *
  1501. * Return: 0 for success, -ERRNO otherwise.
  1502. */
  1503. int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
  1504. {
  1505. struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
  1506. struct cpc_register_resource *desired_reg, *min_perf_reg, *max_perf_reg;
  1507. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
  1508. struct cppc_pcc_data *pcc_ss_data = NULL;
  1509. int ret = 0;
  1510. if (!cpc_desc) {
  1511. pr_debug("No CPC descriptor for CPU:%d\n", cpu);
  1512. return -ENODEV;
  1513. }
  1514. desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
  1515. min_perf_reg = &cpc_desc->cpc_regs[MIN_PERF];
  1516. max_perf_reg = &cpc_desc->cpc_regs[MAX_PERF];
  1517. /*
  1518. * This is Phase-I where we want to write to CPC registers
  1519. * -> We want all CPUs to be able to execute this phase in parallel
  1520. *
  1521. * Since read_lock can be acquired by multiple CPUs simultaneously we
  1522. * achieve that goal here
  1523. */
  1524. if (CPC_IN_PCC(desired_reg) || CPC_IN_PCC(min_perf_reg) || CPC_IN_PCC(max_perf_reg)) {
  1525. if (pcc_ss_id < 0) {
  1526. pr_debug("Invalid pcc_ss_id\n");
  1527. return -ENODEV;
  1528. }
  1529. pcc_ss_data = pcc_data[pcc_ss_id];
  1530. down_read(&pcc_ss_data->pcc_lock); /* BEGIN Phase-I */
  1531. if (pcc_ss_data->platform_owns_pcc) {
  1532. ret = check_pcc_chan(pcc_ss_id, false);
  1533. if (ret) {
  1534. up_read(&pcc_ss_data->pcc_lock);
  1535. return ret;
  1536. }
  1537. }
  1538. /*
  1539. * Update the pending_write to make sure a PCC CMD_READ will not
  1540. * arrive and steal the channel during the switch to write lock
  1541. */
  1542. pcc_ss_data->pending_pcc_write_cmd = true;
  1543. cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt;
  1544. cpc_desc->write_cmd_status = 0;
  1545. }
  1546. cpc_write(cpu, desired_reg, perf_ctrls->desired_perf);
  1547. /*
  1548. * Only write if min_perf and max_perf not zero. Some drivers pass zero
  1549. * value to min and max perf, but they don't mean to set the zero value,
  1550. * they just don't want to write to those registers.
  1551. */
  1552. if (perf_ctrls->min_perf)
  1553. cpc_write(cpu, min_perf_reg, perf_ctrls->min_perf);
  1554. if (perf_ctrls->max_perf)
  1555. cpc_write(cpu, max_perf_reg, perf_ctrls->max_perf);
  1556. if (CPC_IN_PCC(desired_reg) || CPC_IN_PCC(min_perf_reg) || CPC_IN_PCC(max_perf_reg))
  1557. up_read(&pcc_ss_data->pcc_lock); /* END Phase-I */
  1558. /*
  1559. * This is Phase-II where we transfer the ownership of PCC to Platform
  1560. *
  1561. * Short Summary: Basically if we think of a group of cppc_set_perf
  1562. * requests that happened in short overlapping interval. The last CPU to
  1563. * come out of Phase-I will enter Phase-II and ring the doorbell.
  1564. *
  1565. * We have the following requirements for Phase-II:
  1566. * 1. We want to execute Phase-II only when there are no CPUs
  1567. * currently executing in Phase-I
  1568. * 2. Once we start Phase-II we want to avoid all other CPUs from
  1569. * entering Phase-I.
  1570. * 3. We want only one CPU among all those who went through Phase-I
  1571. * to run phase-II
  1572. *
  1573. * If write_trylock fails to get the lock and doesn't transfer the
  1574. * PCC ownership to the platform, then one of the following will be TRUE
  1575. * 1. There is at-least one CPU in Phase-I which will later execute
  1576. * write_trylock, so the CPUs in Phase-I will be responsible for
  1577. * executing the Phase-II.
  1578. * 2. Some other CPU has beaten this CPU to successfully execute the
  1579. * write_trylock and has already acquired the write_lock. We know for a
  1580. * fact it (other CPU acquiring the write_lock) couldn't have happened
  1581. * before this CPU's Phase-I as we held the read_lock.
  1582. * 3. Some other CPU executing pcc CMD_READ has stolen the
  1583. * down_write, in which case, send_pcc_cmd will check for pending
  1584. * CMD_WRITE commands by checking the pending_pcc_write_cmd.
  1585. * So this CPU can be certain that its request will be delivered
  1586. * So in all cases, this CPU knows that its request will be delivered
  1587. * by another CPU and can return
  1588. *
  1589. * After getting the down_write we still need to check for
  1590. * pending_pcc_write_cmd to take care of the following scenario
  1591. * The thread running this code could be scheduled out between
  1592. * Phase-I and Phase-II. Before it is scheduled back on, another CPU
  1593. * could have delivered the request to Platform by triggering the
  1594. * doorbell and transferred the ownership of PCC to platform. So this
  1595. * avoids triggering an unnecessary doorbell and more importantly before
  1596. * triggering the doorbell it makes sure that the PCC channel ownership
  1597. * is still with OSPM.
  1598. * pending_pcc_write_cmd can also be cleared by a different CPU, if
  1599. * there was a pcc CMD_READ waiting on down_write and it steals the lock
  1600. * before the pcc CMD_WRITE is completed. send_pcc_cmd checks for this
  1601. * case during a CMD_READ and if there are pending writes it delivers
  1602. * the write command before servicing the read command
  1603. */
  1604. if (CPC_IN_PCC(desired_reg) || CPC_IN_PCC(min_perf_reg) || CPC_IN_PCC(max_perf_reg)) {
  1605. if (down_write_trylock(&pcc_ss_data->pcc_lock)) {/* BEGIN Phase-II */
  1606. /* Update only if there are pending write commands */
  1607. if (pcc_ss_data->pending_pcc_write_cmd)
  1608. send_pcc_cmd(pcc_ss_id, CMD_WRITE);
  1609. up_write(&pcc_ss_data->pcc_lock); /* END Phase-II */
  1610. } else
  1611. /* Wait until pcc_write_cnt is updated by send_pcc_cmd */
  1612. wait_event(pcc_ss_data->pcc_write_wait_q,
  1613. cpc_desc->write_cmd_id != pcc_ss_data->pcc_write_cnt);
  1614. /* send_pcc_cmd updates the status in case of failure */
  1615. ret = cpc_desc->write_cmd_status;
  1616. }
  1617. return ret;
  1618. }
  1619. EXPORT_SYMBOL_GPL(cppc_set_perf);
  1620. /**
  1621. * cppc_get_transition_latency - returns frequency transition latency in ns
  1622. * @cpu_num: CPU number for per_cpu().
  1623. *
  1624. * ACPI CPPC does not explicitly specify how a platform can specify the
  1625. * transition latency for performance change requests. The closest we have
  1626. * is the timing information from the PCCT tables which provides the info
  1627. * on the number and frequency of PCC commands the platform can handle.
  1628. *
  1629. * If desired_reg is in the SystemMemory or SystemIo ACPI address space,
  1630. * then assume there is no latency.
  1631. */
  1632. int cppc_get_transition_latency(int cpu_num)
  1633. {
  1634. /*
  1635. * Expected transition latency is based on the PCCT timing values
  1636. * Below are definition from ACPI spec:
  1637. * pcc_nominal- Expected latency to process a command, in microseconds
  1638. * pcc_mpar - The maximum number of periodic requests that the subspace
  1639. * channel can support, reported in commands per minute. 0
  1640. * indicates no limitation.
  1641. * pcc_mrtt - The minimum amount of time that OSPM must wait after the
  1642. * completion of a command before issuing the next command,
  1643. * in microseconds.
  1644. */
  1645. struct cpc_desc *cpc_desc;
  1646. struct cpc_register_resource *desired_reg;
  1647. int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu_num);
  1648. struct cppc_pcc_data *pcc_ss_data;
  1649. int latency_ns = 0;
  1650. cpc_desc = per_cpu(cpc_desc_ptr, cpu_num);
  1651. if (!cpc_desc)
  1652. return -ENODATA;
  1653. desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
  1654. if (CPC_IN_SYSTEM_MEMORY(desired_reg) || CPC_IN_SYSTEM_IO(desired_reg))
  1655. return 0;
  1656. if (!CPC_IN_PCC(desired_reg) || pcc_ss_id < 0)
  1657. return -ENODATA;
  1658. pcc_ss_data = pcc_data[pcc_ss_id];
  1659. if (pcc_ss_data->pcc_mpar)
  1660. latency_ns = 60 * (1000 * 1000 * 1000 / pcc_ss_data->pcc_mpar);
  1661. latency_ns = max_t(int, latency_ns, pcc_ss_data->pcc_nominal * 1000);
  1662. latency_ns = max_t(int, latency_ns, pcc_ss_data->pcc_mrtt * 1000);
  1663. return latency_ns;
  1664. }
  1665. EXPORT_SYMBOL_GPL(cppc_get_transition_latency);
  1666. /* Minimum struct length needed for the DMI processor entry we want */
  1667. #define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48
  1668. /* Offset in the DMI processor structure for the max frequency */
  1669. #define DMI_PROCESSOR_MAX_SPEED 0x14
  1670. /* Callback function used to retrieve the max frequency from DMI */
  1671. static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
  1672. {
  1673. const u8 *dmi_data = (const u8 *)dm;
  1674. u16 *mhz = (u16 *)private;
  1675. if (dm->type == DMI_ENTRY_PROCESSOR &&
  1676. dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
  1677. u16 val = (u16)get_unaligned((const u16 *)
  1678. (dmi_data + DMI_PROCESSOR_MAX_SPEED));
  1679. *mhz = umax(val, *mhz);
  1680. }
  1681. }
  1682. /* Look up the max frequency in DMI */
  1683. static u64 cppc_get_dmi_max_khz(void)
  1684. {
  1685. u16 mhz = 0;
  1686. dmi_walk(cppc_find_dmi_mhz, &mhz);
  1687. /*
  1688. * Real stupid fallback value, just in case there is no
  1689. * actual value set.
  1690. */
  1691. mhz = mhz ? mhz : 1;
  1692. return KHZ_PER_MHZ * mhz;
  1693. }
  1694. /*
  1695. * If CPPC lowest_freq and nominal_freq registers are exposed then we can
  1696. * use them to convert perf to freq and vice versa. The conversion is
  1697. * extrapolated as an affine function passing by the 2 points:
  1698. * - (Low perf, Low freq)
  1699. * - (Nominal perf, Nominal freq)
  1700. */
  1701. unsigned int cppc_perf_to_khz(struct cppc_perf_caps *caps, unsigned int perf)
  1702. {
  1703. s64 retval, offset = 0;
  1704. static u64 max_khz;
  1705. u64 mul, div;
  1706. if (caps->lowest_freq && caps->nominal_freq) {
  1707. /* Avoid special case when nominal_freq is equal to lowest_freq */
  1708. if (caps->lowest_freq == caps->nominal_freq) {
  1709. mul = caps->nominal_freq;
  1710. div = caps->nominal_perf;
  1711. } else {
  1712. mul = caps->nominal_freq - caps->lowest_freq;
  1713. div = caps->nominal_perf - caps->lowest_perf;
  1714. }
  1715. mul *= KHZ_PER_MHZ;
  1716. offset = caps->nominal_freq * KHZ_PER_MHZ -
  1717. div64_u64(caps->nominal_perf * mul, div);
  1718. } else {
  1719. if (!max_khz)
  1720. max_khz = cppc_get_dmi_max_khz();
  1721. mul = max_khz;
  1722. div = caps->highest_perf;
  1723. }
  1724. retval = offset + div64_u64(perf * mul, div);
  1725. if (retval >= 0)
  1726. return retval;
  1727. return 0;
  1728. }
  1729. EXPORT_SYMBOL_GPL(cppc_perf_to_khz);
  1730. unsigned int cppc_khz_to_perf(struct cppc_perf_caps *caps, unsigned int freq)
  1731. {
  1732. s64 retval, offset = 0;
  1733. static u64 max_khz;
  1734. u64 mul, div;
  1735. if (caps->lowest_freq && caps->nominal_freq) {
  1736. /* Avoid special case when nominal_freq is equal to lowest_freq */
  1737. if (caps->lowest_freq == caps->nominal_freq) {
  1738. mul = caps->nominal_perf;
  1739. div = caps->nominal_freq;
  1740. } else {
  1741. mul = caps->nominal_perf - caps->lowest_perf;
  1742. div = caps->nominal_freq - caps->lowest_freq;
  1743. }
  1744. /*
  1745. * We don't need to convert to kHz for computing offset and can
  1746. * directly use nominal_freq and lowest_freq as the div64_u64
  1747. * will remove the frequency unit.
  1748. */
  1749. offset = caps->nominal_perf -
  1750. div64_u64(caps->nominal_freq * mul, div);
  1751. /* But we need it for computing the perf level. */
  1752. div *= KHZ_PER_MHZ;
  1753. } else {
  1754. if (!max_khz)
  1755. max_khz = cppc_get_dmi_max_khz();
  1756. mul = caps->highest_perf;
  1757. div = max_khz;
  1758. }
  1759. retval = offset + div64_u64(freq * mul, div);
  1760. if (retval >= 0)
  1761. return retval;
  1762. return 0;
  1763. }
  1764. EXPORT_SYMBOL_GPL(cppc_khz_to_perf);