1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2017 Linaro Ltd. 5 */ 6 #include <linux/slab.h> 7 #include <linux/mutex.h> 8 #include <linux/list.h> 9 #include <linux/completion.h> 10 #include <linux/platform_device.h> 11 #include <linux/videodev2.h> 12 13 #include "core.h" 14 #include "hfi.h" 15 #include "hfi_cmds.h" 16 #include "hfi_venus.h" 17 18 #define TIMEOUT msecs_to_jiffies(1000) 19 20 static u32 to_codec_type(u32 pixfmt) 21 { 22 switch (pixfmt) { 23 case V4L2_PIX_FMT_H264: 24 case V4L2_PIX_FMT_H264_NO_SC: 25 return HFI_VIDEO_CODEC_H264; 26 case V4L2_PIX_FMT_H263: 27 return HFI_VIDEO_CODEC_H263; 28 case V4L2_PIX_FMT_MPEG1: 29 return HFI_VIDEO_CODEC_MPEG1; 30 case V4L2_PIX_FMT_MPEG2: 31 return HFI_VIDEO_CODEC_MPEG2; 32 case V4L2_PIX_FMT_MPEG4: 33 return HFI_VIDEO_CODEC_MPEG4; 34 case V4L2_PIX_FMT_VC1_ANNEX_G: 35 case V4L2_PIX_FMT_VC1_ANNEX_L: 36 return HFI_VIDEO_CODEC_VC1; 37 case V4L2_PIX_FMT_VP8: 38 return HFI_VIDEO_CODEC_VP8; 39 case V4L2_PIX_FMT_VP9: 40 return HFI_VIDEO_CODEC_VP9; 41 case V4L2_PIX_FMT_XVID: 42 return HFI_VIDEO_CODEC_DIVX; 43 case V4L2_PIX_FMT_HEVC: 44 return HFI_VIDEO_CODEC_HEVC; 45 default: 46 return 0; 47 } 48 } 49 50 int hfi_core_init(struct venus_core *core) 51 { 52 int ret = 0; 53 54 mutex_lock(&core->lock); 55 56 if (core->state >= CORE_INIT) 57 goto unlock; 58 59 reinit_completion(&core->done); 60 61 ret = core->ops->core_init(core); 62 if (ret) 63 goto unlock; 64 65 ret = wait_for_completion_timeout(&core->done, TIMEOUT); 66 if (!ret) { 67 ret = -ETIMEDOUT; 68 goto unlock; 69 } 70 71 ret = 0; 72 73 if (core->error != HFI_ERR_NONE) { 74 ret = -EIO; 75 goto unlock; 76 } 77 78 core->state = CORE_INIT; 79 unlock: 80 mutex_unlock(&core->lock); 81 return ret; 82 } 83 84 int hfi_core_deinit(struct venus_core *core, bool blocking) 85 { 86 int ret = 0, empty; 87 88 mutex_lock(&core->lock); 89 90 if (core->state == CORE_UNINIT) 91 goto unlock; 92 93 empty = list_empty(&core->instances); 94 95 if (!empty && !blocking) { 96 ret = -EBUSY; 97 goto unlock; 98 } 99 100 if (!empty) { 101 mutex_unlock(&core->lock); 102 wait_var_event(&core->insts_count, 103 !atomic_read(&core->insts_count)); 104 mutex_lock(&core->lock); 105 } 106 107 ret = core->ops->core_deinit(core); 108 109 if (!ret) 110 core->state = CORE_UNINIT; 111 112 unlock: 113 mutex_unlock(&core->lock); 114 return ret; 115 } 116 117 int hfi_core_suspend(struct venus_core *core) 118 { 119 if (core->state != CORE_INIT) 120 return 0; 121 122 return core->ops->suspend(core); 123 } 124 125 int hfi_core_resume(struct venus_core *core, bool force) 126 { 127 if (!force && core->state != CORE_INIT) 128 return 0; 129 130 return core->ops->resume(core); 131 } 132 133 int hfi_core_trigger_ssr(struct venus_core *core, u32 type) 134 { 135 return core->ops->core_trigger_ssr(core, type); 136 } 137 138 int hfi_core_ping(struct venus_core *core) 139 { 140 int ret; 141 142 mutex_lock(&core->lock); 143 144 ret = core->ops->core_ping(core, 0xbeef); 145 if (ret) 146 goto unlock; 147 148 ret = wait_for_completion_timeout(&core->done, TIMEOUT); 149 if (!ret) { 150 ret = -ETIMEDOUT; 151 goto unlock; 152 } 153 ret = 0; 154 if (core->error != HFI_ERR_NONE) 155 ret = -ENODEV; 156 unlock: 157 mutex_unlock(&core->lock); 158 return ret; 159 } 160 161 static int wait_session_msg(struct venus_inst *inst) 162 { 163 int ret; 164 165 ret = wait_for_completion_timeout(&inst->done, TIMEOUT); 166 if (!ret) 167 return -ETIMEDOUT; 168 169 if (inst->error != HFI_ERR_NONE) 170 return -EIO; 171 172 return 0; 173 } 174 175 int hfi_session_create(struct venus_inst *inst, const struct hfi_inst_ops *ops) 176 { 177 struct venus_core *core = inst->core; 178 179 if (!ops) 180 return -EINVAL; 181 182 inst->state = INST_UNINIT; 183 init_completion(&inst->done); 184 inst->ops = ops; 185 186 mutex_lock(&core->lock); 187 list_add_tail(&inst->list, &core->instances); 188 atomic_inc(&core->insts_count); 189 mutex_unlock(&core->lock); 190 191 return 0; 192 } 193 EXPORT_SYMBOL_GPL(hfi_session_create); 194 195 int hfi_session_init(struct venus_inst *inst, u32 pixfmt) 196 { 197 struct venus_core *core = inst->core; 198 const struct hfi_ops *ops = core->ops; 199 int ret; 200 201 inst->hfi_codec = to_codec_type(pixfmt); 202 reinit_completion(&inst->done); 203 204 ret = ops->session_init(inst, inst->session_type, inst->hfi_codec); 205 if (ret) 206 return ret; 207 208 ret = wait_session_msg(inst); 209 if (ret) 210 return ret; 211 212 inst->state = INST_INIT; 213 214 return 0; 215 } 216 EXPORT_SYMBOL_GPL(hfi_session_init); 217 218 void hfi_session_destroy(struct venus_inst *inst) 219 { 220 struct venus_core *core = inst->core; 221 222 mutex_lock(&core->lock); 223 list_del_init(&inst->list); 224 if (atomic_dec_and_test(&core->insts_count)) 225 wake_up_var(&core->insts_count); 226 mutex_unlock(&core->lock); 227 } 228 EXPORT_SYMBOL_GPL(hfi_session_destroy); 229 230 int hfi_session_deinit(struct venus_inst *inst) 231 { 232 const struct hfi_ops *ops = inst->core->ops; 233 int ret; 234 235 if (inst->state == INST_UNINIT) 236 return 0; 237 238 if (inst->state < INST_INIT) 239 return -EINVAL; 240 241 reinit_completion(&inst->done); 242 243 ret = ops->session_end(inst); 244 if (ret) 245 return ret; 246 247 ret = wait_session_msg(inst); 248 if (ret) 249 return ret; 250 251 inst->state = INST_UNINIT; 252 253 return 0; 254 } 255 EXPORT_SYMBOL_GPL(hfi_session_deinit); 256 257 int hfi_session_start(struct venus_inst *inst) 258 { 259 const struct hfi_ops *ops = inst->core->ops; 260 int ret; 261 262 if (inst->state != INST_LOAD_RESOURCES) 263 return -EINVAL; 264 265 reinit_completion(&inst->done); 266 267 ret = ops->session_start(inst); 268 if (ret) 269 return ret; 270 271 ret = wait_session_msg(inst); 272 if (ret) 273 return ret; 274 275 inst->state = INST_START; 276 277 return 0; 278 } 279 280 int hfi_session_stop(struct venus_inst *inst) 281 { 282 const struct hfi_ops *ops = inst->core->ops; 283 int ret; 284 285 if (inst->state != INST_START) 286 return -EINVAL; 287 288 reinit_completion(&inst->done); 289 290 ret = ops->session_stop(inst); 291 if (ret) 292 return ret; 293 294 ret = wait_session_msg(inst); 295 if (ret) 296 return ret; 297 298 inst->state = INST_STOP; 299 300 return 0; 301 } 302 303 int hfi_session_continue(struct venus_inst *inst) 304 { 305 struct venus_core *core = inst->core; 306 307 if (core->res->hfi_version == HFI_VERSION_1XX) 308 return 0; 309 310 return core->ops->session_continue(inst); 311 } 312 EXPORT_SYMBOL_GPL(hfi_session_continue); 313 314 int hfi_session_abort(struct venus_inst *inst) 315 { 316 const struct hfi_ops *ops = inst->core->ops; 317 int ret; 318 319 reinit_completion(&inst->done); 320 321 ret = ops->session_abort(inst); 322 if (ret) 323 return ret; 324 325 ret = wait_session_msg(inst); 326 if (ret) 327 return ret; 328 329 return 0; 330 } 331 332 int hfi_session_load_res(struct venus_inst *inst) 333 { 334 const struct hfi_ops *ops = inst->core->ops; 335 int ret; 336 337 if (inst->state != INST_INIT) 338 return -EINVAL; 339 340 reinit_completion(&inst->done); 341 342 ret = ops->session_load_res(inst); 343 if (ret) 344 return ret; 345 346 ret = wait_session_msg(inst); 347 if (ret) 348 return ret; 349 350 inst->state = INST_LOAD_RESOURCES; 351 352 return 0; 353 } 354 355 int hfi_session_unload_res(struct venus_inst *inst) 356 { 357 const struct hfi_ops *ops = inst->core->ops; 358 int ret; 359 360 if (inst->state != INST_STOP) 361 return -EINVAL; 362 363 reinit_completion(&inst->done); 364 365 ret = ops->session_release_res(inst); 366 if (ret) 367 return ret; 368 369 ret = wait_session_msg(inst); 370 if (ret) 371 return ret; 372 373 inst->state = INST_RELEASE_RESOURCES; 374 375 return 0; 376 } 377 378 int hfi_session_flush(struct venus_inst *inst) 379 { 380 const struct hfi_ops *ops = inst->core->ops; 381 int ret; 382 383 reinit_completion(&inst->done); 384 385 ret = ops->session_flush(inst, HFI_FLUSH_ALL); 386 if (ret) 387 return ret; 388 389 ret = wait_session_msg(inst); 390 if (ret) 391 return ret; 392 393 return 0; 394 } 395 EXPORT_SYMBOL_GPL(hfi_session_flush); 396 397 int hfi_session_set_buffers(struct venus_inst *inst, struct hfi_buffer_desc *bd) 398 { 399 const struct hfi_ops *ops = inst->core->ops; 400 401 return ops->session_set_buffers(inst, bd); 402 } 403 404 int hfi_session_unset_buffers(struct venus_inst *inst, 405 struct hfi_buffer_desc *bd) 406 { 407 const struct hfi_ops *ops = inst->core->ops; 408 int ret; 409 410 reinit_completion(&inst->done); 411 412 ret = ops->session_unset_buffers(inst, bd); 413 if (ret) 414 return ret; 415 416 if (!bd->response_required) 417 return 0; 418 419 ret = wait_session_msg(inst); 420 if (ret) 421 return ret; 422 423 return 0; 424 } 425 426 int hfi_session_get_property(struct venus_inst *inst, u32 ptype, 427 union hfi_get_property *hprop) 428 { 429 const struct hfi_ops *ops = inst->core->ops; 430 int ret; 431 432 if (inst->state < INST_INIT || inst->state >= INST_STOP) 433 return -EINVAL; 434 435 reinit_completion(&inst->done); 436 437 ret = ops->session_get_property(inst, ptype); 438 if (ret) 439 return ret; 440 441 ret = wait_session_msg(inst); 442 if (ret) 443 return ret; 444 445 *hprop = inst->hprop; 446 447 return 0; 448 } 449 EXPORT_SYMBOL_GPL(hfi_session_get_property); 450 451 int hfi_session_set_property(struct venus_inst *inst, u32 ptype, void *pdata) 452 { 453 const struct hfi_ops *ops = inst->core->ops; 454 455 if (inst->state < INST_INIT || inst->state >= INST_STOP) 456 return -EINVAL; 457 458 return ops->session_set_property(inst, ptype, pdata); 459 } 460 EXPORT_SYMBOL_GPL(hfi_session_set_property); 461 462 int hfi_session_process_buf(struct venus_inst *inst, struct hfi_frame_data *fd) 463 { 464 const struct hfi_ops *ops = inst->core->ops; 465 466 if (fd->buffer_type == HFI_BUFFER_INPUT) 467 return ops->session_etb(inst, fd); 468 else if (fd->buffer_type == HFI_BUFFER_OUTPUT || 469 fd->buffer_type == HFI_BUFFER_OUTPUT2) 470 return ops->session_ftb(inst, fd); 471 472 return -EINVAL; 473 } 474 EXPORT_SYMBOL_GPL(hfi_session_process_buf); 475 476 irqreturn_t hfi_isr_thread(int irq, void *dev_id) 477 { 478 struct venus_core *core = dev_id; 479 480 return core->ops->isr_thread(core); 481 } 482 483 irqreturn_t hfi_isr(int irq, void *dev) 484 { 485 struct venus_core *core = dev; 486 487 return core->ops->isr(core); 488 } 489 490 int hfi_create(struct venus_core *core, const struct hfi_core_ops *ops) 491 { 492 int ret; 493 494 if (!ops) 495 return -EINVAL; 496 497 atomic_set(&core->insts_count, 0); 498 core->core_ops = ops; 499 core->state = CORE_UNINIT; 500 init_completion(&core->done); 501 pkt_set_version(core->res->hfi_version); 502 ret = venus_hfi_create(core); 503 504 return ret; 505 } 506 507 void hfi_destroy(struct venus_core *core) 508 { 509 venus_hfi_destroy(core); 510 } 511