| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Qualcomm Protection Domain mapper
- *
- * Copyright (c) 2023 Linaro Ltd.
- */
- #include <linux/auxiliary_bus.h>
- #include <linux/kernel.h>
- #include <linux/mod_devicetable.h>
- #include <linux/module.h>
- #include <linux/of.h>
- #include <linux/refcount.h>
- #include <linux/slab.h>
- #include <linux/soc/qcom/qmi.h>
- #include "pdr_internal.h"
- #define SERVREG_QMI_VERSION 0x101
- #define SERVREG_QMI_INSTANCE 0
- #define TMS_SERVREG_SERVICE "tms/servreg"
- struct qcom_pdm_domain_data {
- const char *domain;
- u32 instance_id;
- /* NULL-terminated array */
- const char * services[];
- };
- struct qcom_pdm_domain {
- struct list_head list;
- const char *name;
- u32 instance_id;
- };
- struct qcom_pdm_service {
- struct list_head list;
- struct list_head domains;
- const char *name;
- };
- struct qcom_pdm_data {
- refcount_t refcnt;
- struct qmi_handle handle;
- struct list_head services;
- };
- static DEFINE_MUTEX(qcom_pdm_mutex); /* protects __qcom_pdm_data */
- static struct qcom_pdm_data *__qcom_pdm_data;
- static struct qcom_pdm_service *qcom_pdm_find(struct qcom_pdm_data *data,
- const char *name)
- {
- struct qcom_pdm_service *service;
- list_for_each_entry(service, &data->services, list) {
- if (!strcmp(service->name, name))
- return service;
- }
- return NULL;
- }
- static int qcom_pdm_add_service_domain(struct qcom_pdm_data *data,
- const char *service_name,
- const char *domain_name,
- u32 instance_id)
- {
- struct qcom_pdm_service *service;
- struct qcom_pdm_domain *domain;
- service = qcom_pdm_find(data, service_name);
- if (service) {
- list_for_each_entry(domain, &service->domains, list) {
- if (!strcmp(domain->name, domain_name))
- return -EBUSY;
- }
- } else {
- service = kzalloc_obj(*service);
- if (!service)
- return -ENOMEM;
- INIT_LIST_HEAD(&service->domains);
- service->name = service_name;
- list_add_tail(&service->list, &data->services);
- }
- domain = kzalloc_obj(*domain);
- if (!domain) {
- if (list_empty(&service->domains)) {
- list_del(&service->list);
- kfree(service);
- }
- return -ENOMEM;
- }
- domain->name = domain_name;
- domain->instance_id = instance_id;
- list_add_tail(&domain->list, &service->domains);
- return 0;
- }
- static int qcom_pdm_add_domain(struct qcom_pdm_data *data,
- const struct qcom_pdm_domain_data *domain)
- {
- int ret;
- int i;
- ret = qcom_pdm_add_service_domain(data,
- TMS_SERVREG_SERVICE,
- domain->domain,
- domain->instance_id);
- if (ret)
- return ret;
- for (i = 0; domain->services[i]; i++) {
- ret = qcom_pdm_add_service_domain(data,
- domain->services[i],
- domain->domain,
- domain->instance_id);
- if (ret)
- return ret;
- }
- return 0;
- }
- static void qcom_pdm_free_domains(struct qcom_pdm_data *data)
- {
- struct qcom_pdm_service *service, *tservice;
- struct qcom_pdm_domain *domain, *tdomain;
- list_for_each_entry_safe(service, tservice, &data->services, list) {
- list_for_each_entry_safe(domain, tdomain, &service->domains, list) {
- list_del(&domain->list);
- kfree(domain);
- }
- list_del(&service->list);
- kfree(service);
- }
- }
- static void qcom_pdm_get_domain_list(struct qmi_handle *qmi,
- struct sockaddr_qrtr *sq,
- struct qmi_txn *txn,
- const void *decoded)
- {
- struct qcom_pdm_data *data = container_of(qmi, struct qcom_pdm_data, handle);
- const struct servreg_get_domain_list_req *req = decoded;
- struct servreg_get_domain_list_resp *rsp;
- struct qcom_pdm_service *service;
- u32 offset;
- int ret;
- rsp = kzalloc_obj(*rsp);
- if (!rsp)
- return;
- offset = req->domain_offset_valid ? req->domain_offset : 0;
- rsp->resp.result = QMI_RESULT_SUCCESS_V01;
- rsp->resp.error = QMI_ERR_NONE_V01;
- rsp->db_rev_count_valid = true;
- rsp->db_rev_count = 1;
- rsp->total_domains_valid = true;
- rsp->total_domains = 0;
- mutex_lock(&qcom_pdm_mutex);
- service = qcom_pdm_find(data, req->service_name);
- if (service) {
- struct qcom_pdm_domain *domain;
- rsp->domain_list_valid = true;
- rsp->domain_list_len = 0;
- list_for_each_entry(domain, &service->domains, list) {
- u32 i = rsp->total_domains++;
- if (i >= offset && i < SERVREG_DOMAIN_LIST_LENGTH) {
- u32 j = rsp->domain_list_len++;
- strscpy(rsp->domain_list[j].name, domain->name,
- sizeof(rsp->domain_list[i].name));
- rsp->domain_list[j].instance = domain->instance_id;
- pr_debug("PDM: found %s / %d\n", domain->name,
- domain->instance_id);
- }
- }
- }
- pr_debug("PDM: service '%s' offset %d returning %d domains (of %d)\n", req->service_name,
- req->domain_offset_valid ? req->domain_offset : -1, rsp->domain_list_len, rsp->total_domains);
- ret = qmi_send_response(qmi, sq, txn, SERVREG_GET_DOMAIN_LIST_REQ,
- SERVREG_GET_DOMAIN_LIST_RESP_MAX_LEN,
- servreg_get_domain_list_resp_ei, rsp);
- if (ret)
- pr_err("Error sending servreg response: %d\n", ret);
- mutex_unlock(&qcom_pdm_mutex);
- kfree(rsp);
- }
- static void qcom_pdm_pfr(struct qmi_handle *qmi,
- struct sockaddr_qrtr *sq,
- struct qmi_txn *txn,
- const void *decoded)
- {
- const struct servreg_loc_pfr_req *req = decoded;
- struct servreg_loc_pfr_resp rsp = {};
- int ret;
- pr_warn_ratelimited("PDM: service '%s' crash: '%s'\n", req->service, req->reason);
- rsp.rsp.result = QMI_RESULT_SUCCESS_V01;
- rsp.rsp.error = QMI_ERR_NONE_V01;
- ret = qmi_send_response(qmi, sq, txn, SERVREG_LOC_PFR_REQ,
- SERVREG_LOC_PFR_RESP_MAX_LEN,
- servreg_loc_pfr_resp_ei, &rsp);
- if (ret)
- pr_err("Error sending servreg response: %d\n", ret);
- }
- static const struct qmi_msg_handler qcom_pdm_msg_handlers[] = {
- {
- .type = QMI_REQUEST,
- .msg_id = SERVREG_GET_DOMAIN_LIST_REQ,
- .ei = servreg_get_domain_list_req_ei,
- .decoded_size = sizeof(struct servreg_get_domain_list_req),
- .fn = qcom_pdm_get_domain_list,
- },
- {
- .type = QMI_REQUEST,
- .msg_id = SERVREG_LOC_PFR_REQ,
- .ei = servreg_loc_pfr_req_ei,
- .decoded_size = sizeof(struct servreg_loc_pfr_req),
- .fn = qcom_pdm_pfr,
- },
- { },
- };
- static const struct qcom_pdm_domain_data adsp_audio_pd = {
- .domain = "msm/adsp/audio_pd",
- .instance_id = 74,
- .services = {
- "avs/audio",
- NULL,
- },
- };
- static const struct qcom_pdm_domain_data adsp_charger_pd = {
- .domain = "msm/adsp/charger_pd",
- .instance_id = 74,
- .services = { NULL },
- };
- static const struct qcom_pdm_domain_data adsp_root_pd = {
- .domain = "msm/adsp/root_pd",
- .instance_id = 74,
- .services = { NULL },
- };
- static const struct qcom_pdm_domain_data adsp_root_pd_pdr = {
- .domain = "msm/adsp/root_pd",
- .instance_id = 74,
- .services = {
- "tms/pdr_enabled",
- NULL,
- },
- };
- static const struct qcom_pdm_domain_data adsp_sensor_pd = {
- .domain = "msm/adsp/sensor_pd",
- .instance_id = 74,
- .services = { NULL },
- };
- static const struct qcom_pdm_domain_data msm8996_adsp_audio_pd = {
- .domain = "msm/adsp/audio_pd",
- .instance_id = 4,
- .services = { NULL },
- };
- static const struct qcom_pdm_domain_data msm8996_adsp_root_pd = {
- .domain = "msm/adsp/root_pd",
- .instance_id = 4,
- .services = { NULL },
- };
- static const struct qcom_pdm_domain_data cdsp_root_pd = {
- .domain = "msm/cdsp/root_pd",
- .instance_id = 76,
- .services = { NULL },
- };
- static const struct qcom_pdm_domain_data slpi_root_pd = {
- .domain = "msm/slpi/root_pd",
- .instance_id = 90,
- .services = { NULL },
- };
- static const struct qcom_pdm_domain_data slpi_sensor_pd = {
- .domain = "msm/slpi/sensor_pd",
- .instance_id = 90,
- .services = { NULL },
- };
- static const struct qcom_pdm_domain_data mpss_root_pd = {
- .domain = "msm/modem/root_pd",
- .instance_id = 180,
- .services = {
- NULL,
- },
- };
- static const struct qcom_pdm_domain_data mpss_root_pd_gps = {
- .domain = "msm/modem/root_pd",
- .instance_id = 180,
- .services = {
- "gps/gps_service",
- NULL,
- },
- };
- static const struct qcom_pdm_domain_data mpss_root_pd_gps_pdr = {
- .domain = "msm/modem/root_pd",
- .instance_id = 180,
- .services = {
- "gps/gps_service",
- "tms/pdr_enabled",
- NULL,
- },
- };
- static const struct qcom_pdm_domain_data msm8996_mpss_root_pd = {
- .domain = "msm/modem/root_pd",
- .instance_id = 100,
- .services = { NULL },
- };
- static const struct qcom_pdm_domain_data mpss_wlan_pd = {
- .domain = "msm/modem/wlan_pd",
- .instance_id = 180,
- .services = {
- "kernel/elf_loader",
- "wlan/fw",
- NULL,
- },
- };
- static const struct qcom_pdm_domain_data *kaanapali_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_sensor_pd,
- &cdsp_root_pd,
- &mpss_root_pd_gps,
- NULL,
- };
- static const struct qcom_pdm_domain_data *msm8996_domains[] = {
- &msm8996_adsp_audio_pd,
- &msm8996_adsp_root_pd,
- &msm8996_mpss_root_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *msm8998_domains[] = {
- &mpss_root_pd,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *qcm2290_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_sensor_pd,
- &mpss_root_pd_gps,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *qcs404_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_sensor_pd,
- &cdsp_root_pd,
- &mpss_root_pd,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sc7180_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd_pdr,
- &adsp_sensor_pd,
- &mpss_root_pd_gps_pdr,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sc7280_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd_pdr,
- &adsp_charger_pd,
- &adsp_sensor_pd,
- &cdsp_root_pd,
- &mpss_root_pd_gps_pdr,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sc8180x_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_charger_pd,
- &cdsp_root_pd,
- &mpss_root_pd_gps,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sc8280xp_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd_pdr,
- &adsp_charger_pd,
- &cdsp_root_pd,
- NULL,
- };
- /* Unlike SDM660, SDM630/636 lack CDSP */
- static const struct qcom_pdm_domain_data *sdm630_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_sensor_pd,
- &mpss_root_pd,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sdm660_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_sensor_pd,
- &cdsp_root_pd,
- &mpss_root_pd,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sdm670_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &cdsp_root_pd,
- &mpss_root_pd,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sdm845_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &cdsp_root_pd,
- &mpss_root_pd,
- &mpss_wlan_pd,
- &slpi_root_pd,
- &slpi_sensor_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sm6115_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_sensor_pd,
- &cdsp_root_pd,
- &mpss_root_pd_gps,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sm6350_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_sensor_pd,
- &cdsp_root_pd,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sm7150_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_sensor_pd,
- &cdsp_root_pd,
- &mpss_root_pd_gps,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sm8150_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &cdsp_root_pd,
- &mpss_root_pd_gps,
- &mpss_wlan_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sm8250_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &cdsp_root_pd,
- &slpi_root_pd,
- &slpi_sensor_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sm8350_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd_pdr,
- &adsp_charger_pd,
- &cdsp_root_pd,
- &mpss_root_pd_gps,
- &slpi_root_pd,
- &slpi_sensor_pd,
- NULL,
- };
- static const struct qcom_pdm_domain_data *sm8550_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_charger_pd,
- &adsp_sensor_pd,
- &cdsp_root_pd,
- &mpss_root_pd_gps,
- NULL,
- };
- static const struct qcom_pdm_domain_data *x1e80100_domains[] = {
- &adsp_audio_pd,
- &adsp_root_pd,
- &adsp_charger_pd,
- &adsp_sensor_pd,
- &cdsp_root_pd,
- NULL,
- };
- static const struct of_device_id qcom_pdm_domains[] __maybe_unused = {
- { .compatible = "qcom,apq8016", .data = NULL, },
- { .compatible = "qcom,apq8064", .data = NULL, },
- { .compatible = "qcom,apq8074", .data = NULL, },
- { .compatible = "qcom,apq8084", .data = NULL, },
- { .compatible = "qcom,apq8096", .data = msm8996_domains, },
- { .compatible = "qcom,kaanapali", .data = kaanapali_domains, },
- { .compatible = "qcom,msm8226", .data = NULL, },
- { .compatible = "qcom,msm8909", .data = NULL, },
- { .compatible = "qcom,msm8916", .data = NULL, },
- { .compatible = "qcom,msm8939", .data = NULL, },
- { .compatible = "qcom,msm8974", .data = NULL, },
- { .compatible = "qcom,msm8996", .data = msm8996_domains, },
- { .compatible = "qcom,msm8998", .data = msm8998_domains, },
- { .compatible = "qcom,qcm2290", .data = qcm2290_domains, },
- { .compatible = "qcom,qcm6490", .data = sc7280_domains, },
- { .compatible = "qcom,qcs404", .data = qcs404_domains, },
- { .compatible = "qcom,sc7180", .data = sc7180_domains, },
- { .compatible = "qcom,sc7280", .data = sc7280_domains, },
- { .compatible = "qcom,sc8180x", .data = sc8180x_domains, },
- { .compatible = "qcom,sc8280xp", .data = sc8280xp_domains, },
- { .compatible = "qcom,sdm630", .data = sdm630_domains, },
- { .compatible = "qcom,sdm636", .data = sdm630_domains, },
- { .compatible = "qcom,sda660", .data = sdm660_domains, },
- { .compatible = "qcom,sdm660", .data = sdm660_domains, },
- { .compatible = "qcom,sdm670", .data = sdm670_domains, },
- { .compatible = "qcom,sdm845", .data = sdm845_domains, },
- { .compatible = "qcom,sm4250", .data = sm6115_domains, },
- { .compatible = "qcom,sm6115", .data = sm6115_domains, },
- { .compatible = "qcom,sm6350", .data = sm6350_domains, },
- { .compatible = "qcom,sm7150", .data = sm7150_domains, },
- { .compatible = "qcom,sm7225", .data = sm6350_domains, },
- { .compatible = "qcom,sm7325", .data = sc7280_domains, },
- { .compatible = "qcom,sm8150", .data = sm8150_domains, },
- { .compatible = "qcom,sm8250", .data = sm8250_domains, },
- { .compatible = "qcom,sm8350", .data = sm8350_domains, },
- { .compatible = "qcom,sm8450", .data = sm8350_domains, },
- { .compatible = "qcom,sm8550", .data = sm8550_domains, },
- { .compatible = "qcom,sm8650", .data = sm8550_domains, },
- { .compatible = "qcom,sm8750", .data = sm8550_domains, },
- { .compatible = "qcom,x1e80100", .data = x1e80100_domains, },
- { .compatible = "qcom,x1p42100", .data = x1e80100_domains, },
- {},
- };
- static void qcom_pdm_stop(struct qcom_pdm_data *data)
- {
- qcom_pdm_free_domains(data);
- /* The server is removed automatically */
- qmi_handle_release(&data->handle);
- kfree(data);
- }
- static struct qcom_pdm_data *qcom_pdm_start(void)
- {
- const struct qcom_pdm_domain_data * const *domains;
- const struct of_device_id *match;
- struct qcom_pdm_data *data;
- struct device_node *root;
- int ret, i;
- root = of_find_node_by_path("/");
- if (!root)
- return ERR_PTR(-ENODEV);
- match = of_match_node(qcom_pdm_domains, root);
- of_node_put(root);
- if (!match) {
- pr_notice("PDM: no support for the platform, userspace daemon might be required.\n");
- return ERR_PTR(-ENODEV);
- }
- domains = match->data;
- if (!domains) {
- pr_debug("PDM: no domains\n");
- return ERR_PTR(-ENODEV);
- }
- data = kzalloc_obj(*data);
- if (!data)
- return ERR_PTR(-ENOMEM);
- INIT_LIST_HEAD(&data->services);
- ret = qmi_handle_init(&data->handle, SERVREG_GET_DOMAIN_LIST_REQ_MAX_LEN,
- NULL, qcom_pdm_msg_handlers);
- if (ret) {
- kfree(data);
- return ERR_PTR(ret);
- }
- refcount_set(&data->refcnt, 1);
- for (i = 0; domains[i]; i++) {
- ret = qcom_pdm_add_domain(data, domains[i]);
- if (ret)
- goto err_stop;
- }
- ret = qmi_add_server(&data->handle, SERVREG_LOCATOR_SERVICE,
- SERVREG_QMI_VERSION, SERVREG_QMI_INSTANCE);
- if (ret) {
- pr_err("PDM: error adding server %d\n", ret);
- goto err_stop;
- }
- return data;
- err_stop:
- qcom_pdm_stop(data);
- return ERR_PTR(ret);
- }
- static int qcom_pdm_probe(struct auxiliary_device *auxdev,
- const struct auxiliary_device_id *id)
- {
- struct qcom_pdm_data *data;
- int ret = 0;
- mutex_lock(&qcom_pdm_mutex);
- if (!__qcom_pdm_data) {
- data = qcom_pdm_start();
- if (IS_ERR(data))
- ret = PTR_ERR(data);
- else
- __qcom_pdm_data = data;
- } else {
- refcount_inc(&__qcom_pdm_data->refcnt);
- }
- auxiliary_set_drvdata(auxdev, __qcom_pdm_data);
- mutex_unlock(&qcom_pdm_mutex);
- return ret;
- }
- static void qcom_pdm_remove(struct auxiliary_device *auxdev)
- {
- struct qcom_pdm_data *data;
- data = auxiliary_get_drvdata(auxdev);
- if (!data)
- return;
- if (refcount_dec_and_mutex_lock(&data->refcnt, &qcom_pdm_mutex)) {
- __qcom_pdm_data = NULL;
- qcom_pdm_stop(data);
- mutex_unlock(&qcom_pdm_mutex);
- }
- }
- static const struct auxiliary_device_id qcom_pdm_table[] = {
- { .name = "qcom_common.pd-mapper" },
- {},
- };
- MODULE_DEVICE_TABLE(auxiliary, qcom_pdm_table);
- static struct auxiliary_driver qcom_pdm_drv = {
- .name = "qcom-pdm-mapper",
- .id_table = qcom_pdm_table,
- .probe = qcom_pdm_probe,
- .remove = qcom_pdm_remove,
- };
- module_auxiliary_driver(qcom_pdm_drv);
- MODULE_DESCRIPTION("Qualcomm Protection Domain Mapper");
- MODULE_LICENSE("GPL");
|