|
Packit |
534379 |
// Copyright(c) 2019-2020, Intel Corporation
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
534379 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
534379 |
// this list of conditions and the following disclaimer.
|
|
Packit |
534379 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
534379 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
534379 |
// and/or other materials provided with the distribution.
|
|
Packit |
534379 |
// * Neither the name of Intel Corporation nor the names of its contributors
|
|
Packit |
534379 |
// may be used to endorse or promote products derived from this software
|
|
Packit |
534379 |
// without specific prior written permission.
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
534379 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
534379 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
534379 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
534379 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
534379 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
534379 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
534379 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
534379 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
534379 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
534379 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
534379 |
#include <config.h>
|
|
Packit |
534379 |
#endif // HAVE_CONFIG_H
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include <uuid/uuid.h>
|
|
Packit |
534379 |
#include <opae/log.h>
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include "bitstream.h"
|
|
Packit |
534379 |
#include "metadatav1.h"
|
|
Packit |
534379 |
#include "bits_utils.h"
|
|
Packit |
534379 |
|
|
Packit |
534379 |
STATIC fpga_result
|
|
Packit |
534379 |
opae_bitstream_parse_accelerator_cluster_v1(json_object *j_cluster,
|
|
Packit |
534379 |
opae_metadata_accelerator_cluster_v1 *cluster)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
fpga_result res;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_get_json_int(j_cluster,
|
|
Packit |
534379 |
"total-contexts",
|
|
Packit |
534379 |
&cluster->total_contexts);
|
|
Packit |
534379 |
if (res != FPGA_OK)
|
|
Packit |
534379 |
return res;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_get_json_string(j_cluster,
|
|
Packit |
534379 |
"name",
|
|
Packit |
534379 |
&cluster->name);
|
|
Packit |
534379 |
if (res != FPGA_OK)
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_get_json_string(j_cluster,
|
|
Packit |
534379 |
"accelerator-type-uuid",
|
|
Packit |
534379 |
&cluster->accelerator_type_uuid);
|
|
Packit |
534379 |
if (res != FPGA_OK)
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return FPGA_OK;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
out_free:
|
|
Packit |
534379 |
if (cluster->name) {
|
|
Packit |
534379 |
free(cluster->name);
|
|
Packit |
534379 |
cluster->name = NULL;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
return res;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
STATIC fpga_result opae_bitstream_parse_afu_image_v1(json_object *j_afu_image,
|
|
Packit |
534379 |
opae_metadata_afu_image_v1 *img,
|
|
Packit |
534379 |
fpga_guid pr_interface_id)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
fpga_result res;
|
|
Packit |
534379 |
json_object *j_accelerator_clusters = NULL;
|
|
Packit |
534379 |
int i = 0;
|
|
Packit |
534379 |
int ival;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_get_json_double(j_afu_image,
|
|
Packit |
534379 |
"clock-frequency-high",
|
|
Packit |
534379 |
&img->clock_frequency_high);
|
|
Packit |
534379 |
if (res != FPGA_OK) {
|
|
Packit |
534379 |
ival = 0;
|
|
Packit |
534379 |
res = opae_bitstream_get_json_int(j_afu_image,
|
|
Packit |
534379 |
"clock-frequency-high",
|
|
Packit |
534379 |
&ival);
|
|
Packit |
534379 |
img->clock_frequency_high = (double)ival;
|
|
Packit |
534379 |
if (res != FPGA_OK) {
|
|
Packit |
534379 |
// Some errant bitstreams omit
|
|
Packit |
534379 |
// the "clock-frequency-high" key.
|
|
Packit |
534379 |
// Allow it to be optional for now.
|
|
Packit |
534379 |
OPAE_MSG("metadata: missing "
|
|
Packit |
534379 |
"\"clock-frequency-high\" key");
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_get_json_double(j_afu_image,
|
|
Packit |
534379 |
"clock-frequency-low",
|
|
Packit |
534379 |
&img->clock_frequency_low);
|
|
Packit |
534379 |
if (res != FPGA_OK) {
|
|
Packit |
534379 |
ival = 0;
|
|
Packit |
534379 |
res = opae_bitstream_get_json_int(j_afu_image,
|
|
Packit |
534379 |
"clock-frequency-low",
|
|
Packit |
534379 |
&ival);
|
|
Packit |
534379 |
img->clock_frequency_low = (double)ival;
|
|
Packit |
534379 |
if (res != FPGA_OK) {
|
|
Packit |
534379 |
// Some errant bitstreams omit
|
|
Packit |
534379 |
// the "clock-frequency-low" key.
|
|
Packit |
534379 |
// Allow it to be optional for now.
|
|
Packit |
534379 |
OPAE_MSG("metadata: missing "
|
|
Packit |
534379 |
"\"clock-frequency-low\" key");
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_get_json_double(j_afu_image,
|
|
Packit |
534379 |
"power",
|
|
Packit |
534379 |
&img->power);
|
|
Packit |
534379 |
if (res != FPGA_OK) {
|
|
Packit |
534379 |
ival = 0;
|
|
Packit |
534379 |
res = opae_bitstream_get_json_int(j_afu_image,
|
|
Packit |
534379 |
"power",
|
|
Packit |
534379 |
&ival);
|
|
Packit |
534379 |
img->power = (double)ival;
|
|
Packit |
534379 |
if (res != FPGA_OK) {
|
|
Packit |
534379 |
// Some errant bitstreams
|
|
Packit |
534379 |
// omit the "power" key.
|
|
Packit |
534379 |
// Allow it to be optional for now.
|
|
Packit |
534379 |
OPAE_MSG("metadata: missing \"power\" key");
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_get_json_int(j_afu_image,
|
|
Packit |
534379 |
"magic-no",
|
|
Packit |
534379 |
&img->magic_no);
|
|
Packit |
534379 |
if (res != FPGA_OK)
|
|
Packit |
534379 |
return res;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (img->magic_no !=
|
|
Packit |
534379 |
OPAE_LEGACY_BITSTREAM_MAGIC) {
|
|
Packit |
534379 |
OPAE_ERR("metadata: invalid GBS magic: %d",
|
|
Packit |
534379 |
img->magic_no);
|
|
Packit |
534379 |
res = FPGA_EXCEPTION;
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_get_json_string(j_afu_image,
|
|
Packit |
534379 |
"interface-uuid",
|
|
Packit |
534379 |
&img->interface_uuid);
|
|
Packit |
534379 |
if (res != FPGA_OK)
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (uuid_parse(img->interface_uuid, pr_interface_id)) {
|
|
Packit |
534379 |
OPAE_ERR("metadata: uuid_parse failed");
|
|
Packit |
534379 |
res = FPGA_EXCEPTION;
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (!json_object_object_get_ex(j_afu_image,
|
|
Packit |
534379 |
"accelerator-clusters",
|
|
Packit |
534379 |
&j_accelerator_clusters)) {
|
|
Packit |
534379 |
OPAE_ERR("metadata: failed to find "
|
|
Packit |
534379 |
"\"accelerator-clusters\" key");
|
|
Packit |
534379 |
res = FPGA_EXCEPTION;
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (!json_object_is_type(j_accelerator_clusters, json_type_array)) {
|
|
Packit |
534379 |
OPAE_ERR("metadata: \"accelerator-clusters\" key not array");
|
|
Packit |
534379 |
res = FPGA_EXCEPTION;
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
img->num_clusters = json_object_array_length(j_accelerator_clusters);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
img->accelerator_clusters =
|
|
Packit |
534379 |
calloc(img->num_clusters,
|
|
Packit |
534379 |
sizeof(opae_metadata_accelerator_cluster_v1));
|
|
Packit |
534379 |
if (!img->accelerator_clusters) {
|
|
Packit |
534379 |
OPAE_ERR("calloc failed");
|
|
Packit |
534379 |
res = FPGA_NO_MEMORY;
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
for (i = 0 ; i < img->num_clusters ; ++i) {
|
|
Packit |
534379 |
json_object *j_cluster =
|
|
Packit |
534379 |
json_object_array_get_idx(j_accelerator_clusters, i);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_parse_accelerator_cluster_v1(j_cluster,
|
|
Packit |
534379 |
&img->accelerator_clusters[i]);
|
|
Packit |
534379 |
if (res != FPGA_OK)
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return FPGA_OK;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
out_free:
|
|
Packit |
534379 |
if (img->interface_uuid) {
|
|
Packit |
534379 |
free(img->interface_uuid);
|
|
Packit |
534379 |
img->interface_uuid = NULL;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
if (img->accelerator_clusters) {
|
|
Packit |
534379 |
int j;
|
|
Packit |
534379 |
for (j = 0 ; j < i ; ++j) {
|
|
Packit |
534379 |
free(img->accelerator_clusters[j].name);
|
|
Packit |
534379 |
free(img->accelerator_clusters[j].accelerator_type_uuid);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
free(img->accelerator_clusters);
|
|
Packit |
534379 |
img->accelerator_clusters = NULL;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
return res;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
opae_bitstream_metadata_v1 *
|
|
Packit |
534379 |
opae_bitstream_parse_metadata_v1(json_object *root,
|
|
Packit |
534379 |
fpga_guid pr_interface_id)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
opae_bitstream_metadata_v1 *md;
|
|
Packit |
534379 |
fpga_result res;
|
|
Packit |
534379 |
json_object *j_afu_image = NULL;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
md = calloc(1, sizeof(opae_bitstream_metadata_v1));
|
|
Packit |
534379 |
if (!md) {
|
|
Packit |
534379 |
OPAE_ERR("calloc failed");
|
|
Packit |
534379 |
return NULL;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
md->version = 1;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_get_json_string(root,
|
|
Packit |
534379 |
"platform-name",
|
|
Packit |
534379 |
&md->platform_name);
|
|
Packit |
534379 |
if (res != FPGA_OK) {
|
|
Packit |
534379 |
// Some errant bitstreams omit the "platform-name" key.
|
|
Packit |
534379 |
// Allow it to be optional for now.
|
|
Packit |
534379 |
OPAE_MSG("metadata: missing \"platform-name\" key");
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (!json_object_object_get_ex(root,
|
|
Packit |
534379 |
"afu-image",
|
|
Packit |
534379 |
&j_afu_image)) {
|
|
Packit |
534379 |
OPAE_ERR("metadata: failed to find \"afu-image\" key");
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
res = opae_bitstream_parse_afu_image_v1(j_afu_image,
|
|
Packit |
534379 |
&md->afu_image,
|
|
Packit |
534379 |
pr_interface_id);
|
|
Packit |
534379 |
if (res != FPGA_OK)
|
|
Packit |
534379 |
goto out_free;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return md;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
out_free:
|
|
Packit |
534379 |
if (md->platform_name)
|
|
Packit |
534379 |
free(md->platform_name);
|
|
Packit |
534379 |
free(md);
|
|
Packit |
534379 |
return NULL;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
void opae_bitstream_release_metadata_v1(opae_bitstream_metadata_v1 *md)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
int i;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (md->afu_image.accelerator_clusters) {
|
|
Packit |
534379 |
|
|
Packit |
534379 |
for (i = 0 ; i < md->afu_image.num_clusters ; ++i) {
|
|
Packit |
534379 |
opae_metadata_accelerator_cluster_v1 *c =
|
|
Packit |
534379 |
&md->afu_image.accelerator_clusters[i];
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (c->name)
|
|
Packit |
534379 |
free(c->name);
|
|
Packit |
534379 |
if (c->accelerator_type_uuid)
|
|
Packit |
534379 |
free(c->accelerator_type_uuid);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
free(md->afu_image.accelerator_clusters);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (md->afu_image.interface_uuid)
|
|
Packit |
534379 |
free(md->afu_image.interface_uuid);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (md->platform_name)
|
|
Packit |
534379 |
free(md->platform_name);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
free(md);
|
|
Packit |
534379 |
}
|