Path: blob/master/drivers/accel/habanalabs/common/habanalabs_drv.c
26436 views
// SPDX-License-Identifier: GPL-2.012/*3* Copyright 2016-2021 HabanaLabs, Ltd.4* All Rights Reserved.5*6*/78#define pr_fmt(fmt) "habanalabs: " fmt910#include "habanalabs.h"11#include "../include/hw_ip/pci/pci_general.h"1213#include <linux/pci.h>14#include <linux/module.h>15#include <linux/vmalloc.h>16#include <linux/version.h>1718#include <drm/drm_accel.h>19#include <drm/drm_drv.h>20#include <drm/drm_ioctl.h>2122#define CREATE_TRACE_POINTS23#include <trace/events/habanalabs.h>2425#define HL_DRIVER_AUTHOR "HabanaLabs Kernel Driver Team"2627#define HL_DRIVER_DESC "Driver for HabanaLabs's AI Accelerators"2829MODULE_AUTHOR(HL_DRIVER_AUTHOR);30MODULE_DESCRIPTION(HL_DRIVER_DESC);31MODULE_LICENSE("GPL v2");3233static int hl_major;34static DEFINE_IDR(hl_devs_idr);35static DEFINE_MUTEX(hl_devs_idr_lock);3637#define HL_DEFAULT_TIMEOUT_LOCKED 30 /* 30 seconds */38#define GAUDI_DEFAULT_TIMEOUT_LOCKED 600 /* 10 minutes */3940static int timeout_locked = HL_DEFAULT_TIMEOUT_LOCKED;41static int reset_on_lockup = 1;42static int memory_scrub;43static ulong boot_error_status_mask = ULONG_MAX;4445module_param(timeout_locked, int, 0444);46MODULE_PARM_DESC(timeout_locked,47"Device lockup timeout in seconds (0 = disabled, default 30s)");4849module_param(reset_on_lockup, int, 0444);50MODULE_PARM_DESC(reset_on_lockup,51"Do device reset on lockup (0 = no, 1 = yes, default yes)");5253module_param(memory_scrub, int, 0444);54MODULE_PARM_DESC(memory_scrub,55"Scrub device memory in various states (0 = no, 1 = yes, default no)");5657module_param(boot_error_status_mask, ulong, 0444);58MODULE_PARM_DESC(boot_error_status_mask,59"Mask of the error status during device CPU boot (If bitX is cleared then error X is masked. Default all 1's)");6061#define PCI_IDS_GOYA 0x000162#define PCI_IDS_GAUDI 0x100063#define PCI_IDS_GAUDI_SEC 0x10106465#define PCI_IDS_GAUDI2 0x10206667static const struct pci_device_id ids[] = {68{ PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GOYA), },69{ PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI), },70{ PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI_SEC), },71{ PCI_DEVICE(PCI_VENDOR_ID_HABANALABS, PCI_IDS_GAUDI2), },72{ 0, }73};74MODULE_DEVICE_TABLE(pci, ids);7576static const struct drm_ioctl_desc hl_drm_ioctls[] = {77DRM_IOCTL_DEF_DRV(HL_INFO, hl_info_ioctl, 0),78DRM_IOCTL_DEF_DRV(HL_CB, hl_cb_ioctl, 0),79DRM_IOCTL_DEF_DRV(HL_CS, hl_cs_ioctl, 0),80DRM_IOCTL_DEF_DRV(HL_WAIT_CS, hl_wait_ioctl, 0),81DRM_IOCTL_DEF_DRV(HL_MEMORY, hl_mem_ioctl, 0),82DRM_IOCTL_DEF_DRV(HL_DEBUG, hl_debug_ioctl, 0),83};8485static const struct file_operations hl_fops = {86.owner = THIS_MODULE,87.open = accel_open,88.release = drm_release,89.unlocked_ioctl = drm_ioctl,90.compat_ioctl = drm_compat_ioctl,91.llseek = noop_llseek,92.mmap = hl_mmap93};9495static const struct drm_driver hl_driver = {96.driver_features = DRIVER_COMPUTE_ACCEL,9798.name = HL_NAME,99.desc = HL_DRIVER_DESC,100.major = LINUX_VERSION_MAJOR,101.minor = LINUX_VERSION_PATCHLEVEL,102.patchlevel = LINUX_VERSION_SUBLEVEL,103104.fops = &hl_fops,105.open = hl_device_open,106.postclose = hl_device_release,107.ioctls = hl_drm_ioctls,108.num_ioctls = ARRAY_SIZE(hl_drm_ioctls)109};110111/*112* get_asic_type - translate device id to asic type113*114* @hdev: pointer to habanalabs device structure.115*116* Translate device id and revision id to asic type.117* In case of unidentified device, return -1118*/119static enum hl_asic_type get_asic_type(struct hl_device *hdev)120{121struct pci_dev *pdev = hdev->pdev;122enum hl_asic_type asic_type = ASIC_INVALID;123124switch (pdev->device) {125case PCI_IDS_GOYA:126asic_type = ASIC_GOYA;127break;128case PCI_IDS_GAUDI:129asic_type = ASIC_GAUDI;130break;131case PCI_IDS_GAUDI_SEC:132asic_type = ASIC_GAUDI_SEC;133break;134case PCI_IDS_GAUDI2:135switch (pdev->revision) {136case REV_ID_A:137asic_type = ASIC_GAUDI2;138break;139case REV_ID_B:140asic_type = ASIC_GAUDI2B;141break;142case REV_ID_C:143asic_type = ASIC_GAUDI2C;144break;145case REV_ID_D:146asic_type = ASIC_GAUDI2D;147break;148default:149break;150}151break;152default:153break;154}155156return asic_type;157}158159static bool is_asic_secured(enum hl_asic_type asic_type)160{161switch (asic_type) {162case ASIC_GAUDI_SEC:163return true;164default:165return false;166}167}168169/*170* hl_device_open() - open function for habanalabs device.171* @ddev: pointer to DRM device structure.172* @file: pointer to DRM file private data structure.173*174* Called when process opens an habanalabs device.175*/176int hl_device_open(struct drm_device *ddev, struct drm_file *file_priv)177{178struct hl_device *hdev = to_hl_device(ddev);179enum hl_device_status status;180struct hl_fpriv *hpriv;181int rc;182183hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);184if (!hpriv)185return -ENOMEM;186187hpriv->hdev = hdev;188mutex_init(&hpriv->notifier_event.lock);189mutex_init(&hpriv->restore_phase_mutex);190mutex_init(&hpriv->ctx_lock);191kref_init(&hpriv->refcount);192193hl_ctx_mgr_init(&hpriv->ctx_mgr);194hl_mem_mgr_init(hpriv->hdev->dev, &hpriv->mem_mgr);195196hpriv->taskpid = get_task_pid(current, PIDTYPE_PID);197198mutex_lock(&hdev->fpriv_list_lock);199200if (!hl_device_operational(hdev, &status)) {201dev_dbg_ratelimited(hdev->dev,202"Can't open %s because it is %s\n",203dev_name(hdev->dev), hdev->status[status]);204205if (status == HL_DEVICE_STATUS_IN_RESET ||206status == HL_DEVICE_STATUS_IN_RESET_AFTER_DEVICE_RELEASE)207rc = -EAGAIN;208else209rc = -EPERM;210211goto out_err;212}213214if (hdev->is_in_dram_scrub) {215dev_dbg_ratelimited(hdev->dev,216"Can't open %s during dram scrub\n",217dev_name(hdev->dev));218rc = -EAGAIN;219goto out_err;220}221222if (hdev->compute_ctx_in_release) {223dev_dbg_ratelimited(hdev->dev,224"Can't open %s because another user is still releasing it\n",225dev_name(hdev->dev));226rc = -EAGAIN;227goto out_err;228}229230if (hdev->is_compute_ctx_active) {231dev_dbg_ratelimited(hdev->dev,232"Can't open %s because another user is working on it\n",233dev_name(hdev->dev));234rc = -EBUSY;235goto out_err;236}237238rc = hl_ctx_create(hdev, hpriv);239if (rc) {240dev_err(hdev->dev, "Failed to create context %d\n", rc);241goto out_err;242}243244list_add(&hpriv->dev_node, &hdev->fpriv_list);245mutex_unlock(&hdev->fpriv_list_lock);246247hdev->asic_funcs->send_device_activity(hdev, true);248249hl_debugfs_add_file(hpriv);250251hl_enable_err_info_capture(&hdev->captured_err_info);252253hdev->open_counter++;254hdev->last_successful_open_jif = jiffies;255hdev->last_successful_open_ktime = ktime_get();256257file_priv->driver_priv = hpriv;258hpriv->file_priv = file_priv;259260return 0;261262out_err:263mutex_unlock(&hdev->fpriv_list_lock);264hl_mem_mgr_fini(&hpriv->mem_mgr, NULL);265hl_mem_mgr_idr_destroy(&hpriv->mem_mgr);266hl_ctx_mgr_fini(hpriv->hdev, &hpriv->ctx_mgr);267mutex_destroy(&hpriv->ctx_lock);268mutex_destroy(&hpriv->restore_phase_mutex);269mutex_destroy(&hpriv->notifier_event.lock);270put_pid(hpriv->taskpid);271272kfree(hpriv);273274return rc;275}276277int hl_device_open_ctrl(struct inode *inode, struct file *filp)278{279struct hl_device *hdev;280struct hl_fpriv *hpriv;281int rc;282283mutex_lock(&hl_devs_idr_lock);284hdev = idr_find(&hl_devs_idr, iminor(inode));285mutex_unlock(&hl_devs_idr_lock);286287if (!hdev) {288pr_err("Couldn't find device %d:%d\n",289imajor(inode), iminor(inode));290return -ENXIO;291}292293hpriv = kzalloc(sizeof(*hpriv), GFP_KERNEL);294if (!hpriv)295return -ENOMEM;296297/* Prevent other routines from reading partial hpriv data by298* initializing hpriv fields before inserting it to the list299*/300hpriv->hdev = hdev;301filp->private_data = hpriv;302303nonseekable_open(inode, filp);304305hpriv->taskpid = get_task_pid(current, PIDTYPE_PID);306307mutex_lock(&hdev->fpriv_ctrl_list_lock);308309if (!hl_ctrl_device_operational(hdev, NULL)) {310dev_dbg_ratelimited(hdev->dev_ctrl,311"Can't open %s because it is disabled\n",312dev_name(hdev->dev_ctrl));313rc = -EPERM;314goto out_err;315}316317list_add(&hpriv->dev_node, &hdev->fpriv_ctrl_list);318mutex_unlock(&hdev->fpriv_ctrl_list_lock);319320return 0;321322out_err:323mutex_unlock(&hdev->fpriv_ctrl_list_lock);324filp->private_data = NULL;325put_pid(hpriv->taskpid);326327kfree(hpriv);328329return rc;330}331332static void set_driver_behavior_per_device(struct hl_device *hdev)333{334hdev->nic_ports_mask = 0;335hdev->fw_components = FW_TYPE_ALL_TYPES;336hdev->cpu_queues_enable = 1;337hdev->pldm = 0;338hdev->hard_reset_on_fw_events = 1;339hdev->bmc_enable = 1;340hdev->reset_on_preboot_fail = 1;341hdev->heartbeat = 1;342}343344static void copy_kernel_module_params_to_device(struct hl_device *hdev)345{346hdev->asic_prop.fw_security_enabled = is_asic_secured(hdev->asic_type);347348hdev->major = hl_major;349hdev->memory_scrub = memory_scrub;350hdev->reset_on_lockup = reset_on_lockup;351hdev->boot_error_status_mask = boot_error_status_mask;352}353354static void fixup_device_params_per_asic(struct hl_device *hdev, int timeout)355{356switch (hdev->asic_type) {357case ASIC_GAUDI:358case ASIC_GAUDI_SEC:359/* If user didn't request a different timeout than the default one, we have360* a different default timeout for Gaudi361*/362if (timeout == HL_DEFAULT_TIMEOUT_LOCKED)363hdev->timeout_jiffies = secs_to_jiffies(GAUDI_DEFAULT_TIMEOUT_LOCKED);364365hdev->reset_upon_device_release = 0;366break;367368case ASIC_GOYA:369hdev->reset_upon_device_release = 0;370break;371372default:373hdev->reset_upon_device_release = 1;374break;375}376}377378static int fixup_device_params(struct hl_device *hdev)379{380int tmp_timeout;381382tmp_timeout = timeout_locked;383384hdev->fw_poll_interval_usec = HL_FW_STATUS_POLL_INTERVAL_USEC;385hdev->fw_comms_poll_interval_usec = HL_FW_STATUS_POLL_INTERVAL_USEC;386387if (tmp_timeout)388hdev->timeout_jiffies = secs_to_jiffies(tmp_timeout);389else390hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;391392hdev->stop_on_err = true;393hdev->reset_info.curr_reset_cause = HL_RESET_CAUSE_UNKNOWN;394hdev->reset_info.prev_reset_trigger = HL_RESET_TRIGGER_DEFAULT;395396/* Enable only after the initialization of the device */397hdev->disabled = true;398399if (!(hdev->fw_components & FW_TYPE_PREBOOT_CPU) &&400(hdev->fw_components & ~FW_TYPE_PREBOOT_CPU)) {401pr_err("Preboot must be set along with other components");402return -EINVAL;403}404405/* If CPU queues not enabled, no way to do heartbeat */406if (!hdev->cpu_queues_enable)407hdev->heartbeat = 0;408fixup_device_params_per_asic(hdev, tmp_timeout);409410return 0;411}412413static int allocate_device_id(struct hl_device *hdev)414{415int id;416417mutex_lock(&hl_devs_idr_lock);418id = idr_alloc(&hl_devs_idr, hdev, 0, HL_MAX_MINORS, GFP_KERNEL);419mutex_unlock(&hl_devs_idr_lock);420421if (id < 0) {422if (id == -ENOSPC)423pr_err("too many devices in the system\n");424return -EBUSY;425}426427hdev->id = id;428429/*430* Firstly initialized with the internal device ID.431* Will be updated later after the DRM device registration to hold the minor ID.432*/433hdev->cdev_idx = hdev->id;434435return 0;436}437438/**439* create_hdev - create habanalabs device instance440*441* @dev: will hold the pointer to the new habanalabs device structure442* @pdev: pointer to the pci device443*444* Allocate memory for habanalabs device and initialize basic fields445* Identify the ASIC type446* Allocate ID (minor) for the device (only for real devices)447*/448static int create_hdev(struct hl_device **dev, struct pci_dev *pdev)449{450struct hl_device *hdev;451int rc;452453*dev = NULL;454455hdev = devm_drm_dev_alloc(&pdev->dev, &hl_driver, struct hl_device, drm);456if (IS_ERR(hdev))457return PTR_ERR(hdev);458459hdev->dev = hdev->drm.dev;460461/* Will be NULL in case of simulator device */462hdev->pdev = pdev;463464/* Assign status description string */465strscpy(hdev->status[HL_DEVICE_STATUS_OPERATIONAL], "operational", HL_STR_MAX);466strscpy(hdev->status[HL_DEVICE_STATUS_IN_RESET], "in reset", HL_STR_MAX);467strscpy(hdev->status[HL_DEVICE_STATUS_MALFUNCTION], "disabled", HL_STR_MAX);468strscpy(hdev->status[HL_DEVICE_STATUS_NEEDS_RESET], "needs reset", HL_STR_MAX);469strscpy(hdev->status[HL_DEVICE_STATUS_IN_DEVICE_CREATION],470"in device creation", HL_STR_MAX);471strscpy(hdev->status[HL_DEVICE_STATUS_IN_RESET_AFTER_DEVICE_RELEASE],472"in reset after device release", HL_STR_MAX);473474475/* First, we must find out which ASIC are we handling. This is needed476* to configure the behavior of the driver (kernel parameters)477*/478hdev->asic_type = get_asic_type(hdev);479if (hdev->asic_type == ASIC_INVALID) {480dev_err(&pdev->dev, "Unsupported ASIC\n");481rc = -ENODEV;482goto out_err;483}484485copy_kernel_module_params_to_device(hdev);486487set_driver_behavior_per_device(hdev);488489fixup_device_params(hdev);490491rc = allocate_device_id(hdev);492if (rc)493goto out_err;494495*dev = hdev;496497return 0;498499out_err:500return rc;501}502503/*504* destroy_hdev - destroy habanalabs device instance505*506* @dev: pointer to the habanalabs device structure507*508*/509static void destroy_hdev(struct hl_device *hdev)510{511/* Remove device from the device list */512mutex_lock(&hl_devs_idr_lock);513idr_remove(&hl_devs_idr, hdev->id);514mutex_unlock(&hl_devs_idr_lock);515516}517518static int hl_pmops_suspend(struct device *dev)519{520struct hl_device *hdev = dev_get_drvdata(dev);521522pr_debug("Going to suspend PCI device\n");523524if (!hdev) {525pr_err("device pointer is NULL in suspend\n");526return 0;527}528529return hl_device_suspend(hdev);530}531532static int hl_pmops_resume(struct device *dev)533{534struct hl_device *hdev = dev_get_drvdata(dev);535536pr_debug("Going to resume PCI device\n");537538if (!hdev) {539pr_err("device pointer is NULL in resume\n");540return 0;541}542543return hl_device_resume(hdev);544}545546/**547* hl_pci_probe - probe PCI habanalabs devices548*549* @pdev: pointer to pci device550* @id: pointer to pci device id structure551*552* Standard PCI probe function for habanalabs device.553* Create a new habanalabs device and initialize it according to the554* device's type555*/556static int hl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)557{558struct hl_device *hdev;559int rc;560561dev_info(&pdev->dev, HL_NAME562" device found [%04x:%04x] (rev %x)\n",563(int)pdev->vendor, (int)pdev->device, (int)pdev->revision);564565rc = create_hdev(&hdev, pdev);566if (rc)567return rc;568569pci_set_drvdata(pdev, hdev);570571rc = hl_device_init(hdev);572if (rc) {573dev_err(&pdev->dev, "Fatal error during habanalabs device init\n");574rc = -ENODEV;575goto disable_device;576}577578return 0;579580disable_device:581pci_set_drvdata(pdev, NULL);582destroy_hdev(hdev);583584return rc;585}586587/*588* hl_pci_remove - remove PCI habanalabs devices589*590* @pdev: pointer to pci device591*592* Standard PCI remove function for habanalabs device593*/594static void hl_pci_remove(struct pci_dev *pdev)595{596struct hl_device *hdev;597598hdev = pci_get_drvdata(pdev);599if (!hdev)600return;601602hl_device_fini(hdev);603pci_set_drvdata(pdev, NULL);604destroy_hdev(hdev);605}606607/**608* hl_pci_err_detected - a PCI bus error detected on this device609*610* @pdev: pointer to pci device611* @state: PCI error type612*613* Called by the PCI subsystem whenever a non-correctable614* PCI bus error is detected615*/616static pci_ers_result_t617hl_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t state)618{619struct hl_device *hdev = pci_get_drvdata(pdev);620enum pci_ers_result result;621622switch (state) {623case pci_channel_io_normal:624dev_warn(hdev->dev, "PCI normal state error detected\n");625return PCI_ERS_RESULT_CAN_RECOVER;626627case pci_channel_io_frozen:628dev_warn(hdev->dev, "PCI frozen state error detected\n");629result = PCI_ERS_RESULT_NEED_RESET;630break;631632case pci_channel_io_perm_failure:633dev_warn(hdev->dev, "PCI failure state error detected\n");634result = PCI_ERS_RESULT_DISCONNECT;635break;636637default:638result = PCI_ERS_RESULT_NONE;639}640641hdev->asic_funcs->halt_engines(hdev, true, false);642643return result;644}645646/**647* hl_pci_err_resume - resume after a PCI slot reset648*649* @pdev: pointer to pci device650*651*/652static void hl_pci_err_resume(struct pci_dev *pdev)653{654struct hl_device *hdev = pci_get_drvdata(pdev);655656dev_warn(hdev->dev, "Resuming device after PCI slot reset\n");657hl_device_resume(hdev);658}659660/**661* hl_pci_err_slot_reset - a PCI slot reset has just happened662*663* @pdev: pointer to pci device664*665* Determine if the driver can recover from the PCI slot reset666*/667static pci_ers_result_t hl_pci_err_slot_reset(struct pci_dev *pdev)668{669struct hl_device *hdev = pci_get_drvdata(pdev);670671dev_warn(hdev->dev, "PCI slot reset detected\n");672673return PCI_ERS_RESULT_RECOVERED;674}675676static void hl_pci_reset_prepare(struct pci_dev *pdev)677{678struct hl_device *hdev;679680hdev = pci_get_drvdata(pdev);681if (!hdev)682return;683684hdev->disabled = true;685}686687static void hl_pci_reset_done(struct pci_dev *pdev)688{689struct hl_device *hdev;690u32 flags;691692hdev = pci_get_drvdata(pdev);693if (!hdev)694return;695696/*697* Schedule a thread to trigger hard reset.698* The reason for this handler, is for rare cases where the driver is up699* and FLR occurs. This is valid only when working with no VM, so FW handles FLR700* and resets the device. FW will go back preboot stage, so driver needs to perform701* hard reset in order to load FW fit again.702*/703flags = HL_DRV_RESET_HARD | HL_DRV_RESET_BYPASS_REQ_TO_FW;704705hl_device_reset(hdev, flags);706}707708static const struct dev_pm_ops hl_pm_ops = {709.suspend = hl_pmops_suspend,710.resume = hl_pmops_resume,711};712713static const struct pci_error_handlers hl_pci_err_handler = {714.error_detected = hl_pci_err_detected,715.slot_reset = hl_pci_err_slot_reset,716.resume = hl_pci_err_resume,717.reset_prepare = hl_pci_reset_prepare,718.reset_done = hl_pci_reset_done,719};720721static struct pci_driver hl_pci_driver = {722.name = HL_NAME,723.id_table = ids,724.probe = hl_pci_probe,725.remove = hl_pci_remove,726.shutdown = hl_pci_remove,727.driver = {728.name = HL_NAME,729.pm = &hl_pm_ops,730.probe_type = PROBE_PREFER_ASYNCHRONOUS,731},732.err_handler = &hl_pci_err_handler,733};734735/*736* hl_init - Initialize the habanalabs kernel driver737*/738static int __init hl_init(void)739{740int rc;741dev_t dev;742743pr_info("loading driver\n");744745rc = alloc_chrdev_region(&dev, 0, HL_MAX_MINORS, HL_NAME);746if (rc < 0) {747pr_err("unable to get major\n");748return rc;749}750751hl_major = MAJOR(dev);752753rc = pci_register_driver(&hl_pci_driver);754if (rc) {755pr_err("failed to register pci device\n");756goto remove_major;757}758759pr_debug("driver loaded\n");760761return 0;762763remove_major:764unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);765return rc;766}767768/*769* hl_exit - Release all resources of the habanalabs kernel driver770*/771static void __exit hl_exit(void)772{773pci_unregister_driver(&hl_pci_driver);774775unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);776777idr_destroy(&hl_devs_idr);778779pr_debug("driver removed\n");780}781782module_init(hl_init);783module_exit(hl_exit);784785786